Last Updated: August 26, 2019
·
150.6K
· phobson

Adding columns to a pandas dataframe

Dataframes in some ways act very similar to Python dictionaries in that you easily add new columns. It's as simple as:

df = pandas.DataFrame.from_csv('my_data.csv')  # fake data
df['diff_A_B'] = df['A'] - df['B']

You can also use the assign method to return a modified copy

df2 = df.assign(diff_col=df['A'] - df['B'])

1 Response
Add your response

This method results in a syntax error. Is there another way to perform this action?

over 1 year ago ·