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'])
Written by Paul Hobson
Related protips
1 Response
This method results in a syntax error. Is there another way to perform this action?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Pandas
Authors
monkee_magic
8.623K
paulofilip3
2.323K
Related Tags
#pandas
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#