我有三个具有相同列名的DataFrame对象,我感兴趣的列“ a”将最终进行排序。而且,一旦将它们组合在一起,我想迭代地访问它们。
df1 = pd.DataFrame(np.random.randn(10, 4), columns=['a','b','c','d'])
df2 = pd.DataFrame(np.random.randn(10, 4), columns=['a','b','c','d'])
df3 = pd.DataFrame(np.random.randn(10, 4), columns=['a','b','c','d'])
df1['src'] = np.array(['str1' for i in range(len(df1))])
df2['src'] = np.array(['str2' for i in range(len(df2))])
df3['src'] = np.array(['str3' for i in range(len(df3))])
combo = pd.concat([df1,df2,df3])
combo.ix[0]
In [117]: combo.ix[0]
Out[117]:
a b c d src
0 1.002482 -1.801156 -0.413474 -0.714292 str1
0 -0.816205 0.670441 0.091646 1.290459 str2
0 0.543803 0.841918 0.281999 1.204264 str3
In [125]: df1.ix[0]
Out[125]:
a 1.002482
b -1.801156
c -0.413474
d -0.714292
src str1
Name: 0, dtype: object
在上面的集合中,一旦连接起来,我将不再具有获取单个项目的能力。每张纸条我要得到3。我想念什么?
最佳答案
您需要将参数ignore_index=True
传递给concat
In [2]:
df1 = pd.DataFrame(np.random.randn(10, 4), columns=['a','b','c','d'])
df2 = pd.DataFrame(np.random.randn(10, 4), columns=['a','b','c','d'])
df3 = pd.DataFrame(np.random.randn(10, 4), columns=['a','b','c','d'])
df1['src'] = np.array(['str1' for i in range(len(df1))])
df2['src'] = np.array(['str2' for i in range(len(df2))])
df3['src'] = np.array(['str3' for i in range(len(df3))])
combo = pd.concat([df1,df2,df3], ignore_index=True)
combo.ix[0]
Out[2]:
a -0.839181
b -0.1489309
c -1.655775
d -0.1682147
src str1
Name: 0, dtype: object
关于python - 串联数据框单标签行选择返回多行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27989737/