0

As you can check in this question Here, the best way to add a column in Pandas 0.16+ is

df = df.assign(new_column = Something)

Where new_column is literaly the name of the new column (even if it is not written as a string).

This is a problem for me, as I want to add many columns, whose name is specified by a variable

I tried this:

for col in df.columns:
    new_col_name = col + "_nancount" 
    test = test.assign(new_col_name = test[col].isna().sum())

It doesn't work: in this way, just one Column is added (and it's named "new_col_name")

The expected result is, given a table with columns ["A", "B", "C"] to have a table with columns ["A", "B", "C", "A_nancount", "B_nancount", "C_nancount"]

How can I do that?

Community
  • 1
  • 1
Federico Dorato
  • 710
  • 9
  • 27

1 Answers1

0

Following the content of this reply Here, I think the best solution is the following:

df = df.assign(**{col1: Something, col2: Something})

In my case:

new_col_dict = {}
for col in df.columns:
    new_col_dict[col + "_nancount"] = test[col].isna().sum()
    test = test.assign(**new_col_dict)
Federico Dorato
  • 710
  • 9
  • 27