To append a new column to a dataframe in Python, you can use the following code:

import pandas as pd

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

# create a new column
new_column = pd.Series([7, 8, 9])

# append new column to dataframe
df['C'] = new_column

print(df)

Output:

   A  B  C
0  1  4  7
1  2  5  8
2  3  6  9

In this example, we first create a sample dataframe with two columns 'A' and 'B'. Then, we create a new column 'C' using a Pandas Series object. Finally, we append the new column to the dataframe using the square bracket notation and assign it the value of the new column.

Pandas DataFrame: Adding New Columns (with Code Examples)

原文地址: https://www.cveoy.top/t/topic/n6qH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录