Home > Blockchain >  Create new dataframe and new column at the same time
Create new dataframe and new column at the same time

Time:10-23

I wonder if we can create new DataFrame and new column at once as below.

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

# Can I combine the 2 rows into 1
new_df = df
new_df['new column']=new_df['col1']*2   new_df['col2']/4

print(new_df)

CodePudding user response:

You can do this with the .assign() method of a data frame:

>>> df
   col1  col2
0     1     3
1     2     4
>>> new_df = df.assign(col3=df["col1"] * 2   df["col2"] / 4)
>>> new_df
   col1  col2  col3
0     1     3  2.75
1     2     4  5.00

CodePudding user response:

If you just want to make the code looks shorter, use assign right after creating a dataframe. The code snippet can look like below:

df = pd.DataFrame(d).assign(new_column=lambda x: x['col1'] * 2   x['col2'] / 4)
  • Related