Assign A New Column In Pandas In A Similar Way As In Pyspark
I have the following dataframe: df = pd.DataFrame([['A', 1],['B', 2],['C', 3]], columns=['index', 'result']) I would like to create a new column, for example multiply the column
Solution 1:
Use DataFrame.assign
:
df = df.assign(result_multiplied = df['result']*2)
Or if column result
is processing in code before is necessary lambda function for processing counted values in column result
:
df = df.assign(result_multiplied = lambda x: x['result']*2)
Sample for see difference column result_multiplied
is count by multiple original df['result']
, for result_multiplied1
is used multiplied column after mul(2)
:
df = df.mul(2).assign(result_multiplied = df['result']*2,
result_multiplied1 = lambda x: x['result']*2)
print (df)
index result result_multiplied result_multiplied1
0 AA 2 2 4
1 BB 4 4 8
2 CC 6 6 12
Post a Comment for "Assign A New Column In Pandas In A Similar Way As In Pyspark"