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?
You can try Series.rename
:
df = pd.concat((df, s.rename('col')), axis=1)