Pythonista anonymous
Pythonista anonymous

Reputation: 8970

Can I get concat() to ignore column names and work only based on the position of the columns?

The docs , at least as of version 0.24.2, specify that pandas.concat can ignore the index, with ignore_index=True, but

Note the index values on the other axes are still respected in the join.

Is there a way to avoid this, i.e. to concatenate based on the position only, and ignoring the names of the columns?

I see two options:

Are there more elegant ways?

For example, if I want to add the series s as an additional row to the dataframe df, I can:

It works, but it seems very "un-pythonic"!

A toy example is below; this example is with a dataframe and a series, but the same concept applies with two dataframes.

import pandas as pd

df=pd.DataFrame()
df['a']=[1]
df['x']='this'
df['y']='that'

s=pd.Series([3,'txt','more txt'])
st=s.to_frame().transpose()
st.columns=df.columns
out= pd.concat( [df, st] , axis=0, ignore_index=True)

Upvotes: 1

Views: 2197

Answers (1)

Alex Fortin
Alex Fortin

Reputation: 2425

In the case of 1 dataframe and 1 series, you can do:

df.loc[df.shape[0], :] = s.values

Upvotes: 1

Related Questions