Pandas Dataframe无法定位具有不同数据类型的列

2024-10-01 13:37:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个跨列的具有不同数据类型的数据帧(但每列中的数据类型相同)。当我尝试将一些新数据定位到第0行时,第一行将被列名替换。你知道吗

但是,当列都是相同的数据类型时,不会发生这种情况。你知道吗

import numpy as np
import pandas as pd

test = pd.DataFrame({'int':[0],
    'float':[0.],
    'str':['z'],})
test.loc[(0)] = {
    'int':0,
    'float':0.1,
    'str':'a',}
test.loc[(1)] = {
    'int':1,
    'float':1.1,
    'str':'b',}

test2 = pd.DataFrame({'float0':[0.],
    'float1':[0.],
    'float2':[0.],})
test2.loc[(0)] = {
    'float0':0.,
    'float1':0.1,
    'float2':0.2,}
test2.loc[(1)] = {
    'float0':1.0,
    'float1':1.1,
    'float2':1.2,}
print(test.to_string())

   int  float  str
0  int  float  str
1    1    1.1    b

print(test2.to_string())

   float0  float1  float2
0     0.0     0.1     0.2
1     1.0     1.1     1.2

Tags: 数据testimportdataframeasfloatlocint