I need to add columns to the DataFrame df
. The values for all new columns should be fixed along all rows of df
:
df = {
"NUM":[1,2],
"WAKE":["M","H"],
"DISTANCE":[780,500]
}
new_df = pd.DataFrame(df)
This is how I tried to add new multiple columns with fixed values.
for column, row in new_df.iterrows():
row["TEMPERATURE"] = 20
row["VISIBILITY"] = 5000
row["WIND"] = 10
This code does not fail, but new columns are not created.
The expected result:
NUM WAKE DISTANCE TEMPERATURE VISIBILITY WIND
1 M 780 20 5000 10
2 H 500 20 5000 10
This is as simple as a single assign
call with a dictionary:
dct = {'TEMPERATURE': 20, 'VISIBILITY': 5000, 'WIND': 10}
new_df2 = new_df.assign(**dct)
new_df2
NUM WAKE DISTANCE TEMPERATURE VISIBILITY WIND
0 1 M 780 20 5000 10
1 2 H 500 20 5000 10
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments