dt4yja
Last Updated: August 30, 2017
·
91.34K
· phobson
7840b7a3579d2a7065d5c0aa804b5b92

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'])
Say Thanks
Respond

1 Response
Add your response

29260

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

6 months ago ·