2014-03-26 41 views
1

在我看来,当我做concat时,熊猫正在返回一个数据帧,而不是一个单一的serie。这给了我一些麻烦..熊猫做一个“真正的”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] 

正如你所看到的问题是,它并没有创造新的指数,我因此没有得到一列而是两个。

我该如何“正确”?

+0

你究竟在干什么?是'df1.combine_first(df2)'你想要什么?熊猫在这里做了一个真正的'concat',这意味着它连接了'df1'和'df2'而不会丢失任何行... – filmor

+0

我觉得你很迷惑“两个系列” - 而且没有任何;这是一列数据帧 - 事实上,默认情况下,串联保留了索引。 – DSM

回答

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 
+0

Excatly! - 非常感谢 – Norfeldt