How to add multiple new columns with a fixed value?

ScalaBoy

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
cs95

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I add two new columns on the basis of the values of multiple other columns in a pandas dataframe?

Add multiple new columns to a data table

How to add multiple columns to a DataFrame?

Find nearest value from multiple columns and add to a new column in Python

Compare values in multiple columns and add a new value in another column in Python

how to add a new feature base on other columns with same value

How to assign a value to a new column in a data frame based on a combination of multiple conditions on multiple columns

Add new column containing the difference with a fixed reference value in R

How to add multiple columns with purrr?

Sql to split row to multiple rows based on a fixed value of multiple columns

How can I check existing columns in a table with the new value before add the new record into the access table?

SAS Add new multiple columns based on other column's value using loop

How to add new columns to dataframe with value taken from another dataframe?

How to add new column, where the value is based on existing columns with awk

How to add fixed underscore if value either exists or not?

How to add 3 new columns?

Check content of multiple columns of one row and add new column with value depending on contents

How to assign a new columns where value are a set of column names based on multiple columns by pandas?

Compair 2 columns and add a value in a new column

How to add multiple columns in current select statement as a new field?

How to add a new pandas column whose value is conditioned on one column, but value depends on other columns?

Add a new column if multiple columns have negative value

Pandas: How to aggregate multiple columns into value counts, generating new columns for each value count?

How to find the maximum value of 3 columns and multiply the other 2 with a fixed factor, then add all together?

How to add multiple new columns with when condition in pyspark dataframe?

Pandas - How to combine multiple columns into new one with list as value?

Add a new column that specify multiple columns name that have value

I have a data frame with multiple columns. How do I group the columns by year, and add a new column that ranks the values of one of the columns?

how to check the value in some columns and add the header to the new column in pandas