连接数据框单标签行选择 returns 多行

Concatenating dataframes single label row selection returns multiple rows

我有三个具有相同列名的 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