Trying to select data from all columns starting with a row from pandas dataframe

I am trying to select all columns starting with a specific row and then populate all null values ​​with a new value. What I'm doing now is turning all the column headers into a list instead.

lifestyle_var = [col for col in list(df) if col.startswith('lifestyle')]

df[lifestyle_var].fillna(1, inplace=True)
+4
source share
2 answers

I had the same problem: https://github.com/pydata/pandas/issues/10342

You can use this command: df.loc[:,lifestyle_var] = df.loc[:,lifestyle_var].fillna(1)

This problem occurs because you are trying to populate a copy of the data frame, not the original data.

+1
source

Try

df.update(df[lifestyle_var].fillna(1))

Cm. .

Example:

import pandas as pd
import numpy as np
data = pd.DataFrame([ [ 1, 2, np.nan ], [ np.nan, np.nan, 6] ], columns=   ['a1', 'b', 'a2'])
vars = [ col for col in list(data) if col.startswith('a')]
data.update(data[vars].fillna(value=1))
0
source

Source: https://habr.com/ru/post/1607857/


All Articles