大熊猫做一个“真正的”联合会

时间:2014-03-26 14:47:13

标签: python pandas

在我看来,当我做concat时,大熊猫正在返回一个包含两个系列而不是一个系列的数据框。这给我带来了一些麻烦......

df1 = pandas.DataFrame(np.random.randn(4, 1), columns=['A'])
df2 = pandas.DataFrame(np.random.randn(4, 1), columns=['A'])

df3 = pandas.concat( [df1, df2] )
print df3
#Trying to isolate the row with the lowest value
print df3.ix[df3['A'].argmin()]

给我这个输出

          A
0 -1.368203
1  0.340653
2 -0.431968
3 -0.354293
0  0.391797
1 -0.263332
2 -1.450046
3  0.162143    
[8 rows x 1 columns]

          A
2 -0.431968
2 -1.450046    
[2 rows x 1 columns]

正如你所看到的那样,问题是它没有创建新的索引,因此我不会得到一行而是两行。

我该怎么做“正确”?

1 个答案:

答案 0 :(得分:3)

您在寻找ignore_index=True吗?

In [8]:

df3 = pandas.concat( [df1, df2] , ignore_index=True)
print df3
#Trying to isolate the row with the lowest value
print df3.ix[df3['A'].argmin()]


          A
0 -0.218089
1 -0.638552
2  0.955099
3  0.508360
4 -0.000249
5  0.125377
6  0.969202
7 -1.112411

[8 rows x 1 columns]
A   -1.112411
Name: 7, dtype: float64