Norfeldt
Norfeldt

Reputation: 9698

pandas do a "true" concat

It seems to me that pandas is returning a data frame with two series instead of one single serie when I do the concat. That gives me some troubles..

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()]

Gives me this output

          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]

As you can see the problem is that it does not create new indices and I therefore do not get one row but two.

How do I do it "correctly" ?

Upvotes: 1

Views: 59

Answers (1)

CT Zhu
CT Zhu

Reputation: 54380

Are you looking for 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

Upvotes: 3

Related Questions