Home > other >  how to change the row of a DataFrame depending on values of one column
how to change the row of a DataFrame depending on values of one column

Time:07-05

I have a huge dataframe with multiple time series like in the link below.

Dataframe with multiple time series

At the end there should be a dataframe with the same values like before except when there is no value (nan) in column g. Shortly said, if there is a value in column g all the other rows should stay the same. If there is a "nan" in column g, all the other values should be also "nan" in this row.There are up to 200 Columns in this dataframe so is it possible to write it in a way without writing the individual column names like in a lot of examples. I tried it with df.iloc and np.where but honestly said, I cannot define the conditions to make it work.

Solution with rows adapted

I hope someone can help me. Thanks in advance.

CodePudding user response:

for index in df.index:
    if np.isnan(df.loc[index, "g"]):
        df.loc[index, :] = np.nan

This should work if I understand your question correctly.

CodePudding user response:

import pandas as pd
import numpy as np

columns = ['Date','a','b','c','d','e','f','g']
data = [
  ['01.06.2022','0,574','0,2342','0,574','0,2342','0,574','0,2342','0,574'],
  ['02.06.2022','0,574','0,2342','0,574','0,2342','0,574','0,2342',np.nan],
  ]


df = pd.DataFrame(data, columns=columns)

rows_to_adapt = list(df.columns)[1:-1]
check_row = ['g']


for col in rows_to_adapt:
    df[col] = np.where(df['g'].isnull(), np.nan, df[col])
  • Related