append a dataframe with a new serie in python

To append a new series to an existing DataFrame in Pandas, you can use the append() method.

Here's an example code:

main.py
import pandas as pd

# create the initial DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# create the new series
new_series = pd.Series([7, 8, 9], name='C')

# append the new series to the existing DataFrame
df = df.append(new_series)

print(df)
262 chars
13 lines

In this code, we first created an initial DataFrame df. Then, we created a new series new_series that we want to append to the DataFrame. We passed the new_series to the append() method of the DataFrame, and stored the result back in df. Finally, we printed df to verify that the new series has been successfully appended.

gistlibby LogSnag