连接数据帧单标签行选择返回多行

时间:2015-01-16 17:44:14

标签: python numpy pandas

我有三个具有相同列名的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分。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您需要将参数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