concat series onto dataframe with column name

vaer-k picture vaer-k · Aug 19, 2016 · Viewed 41.9k times · Source

I want to add a Series (s) to a Pandas DataFrame (df) as a new column. The series has more values than there are rows in the dataframe, so I am using the concat method along axis 1.

df = pd.concat((df, s), axis=1)

This works, but the new column of the dataframe representing the series is given an arbitrary numerical column name, and I would like this column to have a specific name instead.

Is there a way to add a series to a dataframe, when the series is longer than the rows of the dataframe, and with a specified column name in the resulting dataframe?

Answer

jezrael picture jezrael · Aug 19, 2016

You can try Series.rename:

df = pd.concat((df, s.rename('col')), axis=1)