Home > OS >  Assign consequential values to a DataFrame from a numpy array based on a condition
Assign consequential values to a DataFrame from a numpy array based on a condition

Time:11-19

The task seems easy but I've been googling and experimenting for hours without any result. I can easily assign a 'static' value in such case or assign a value if I have two columns in the same DataFrame (of the same length, ofc) but I'm stuck with this situation.

I need to assign a consequential value to a pandas DataFrame column from a numpy array based on a condition when the sizes of the DataFrame and the numpy.array are different.

Here is the example:

import pandas as pd
import numpy as np

if __name__ == "__main__":
    df = pd.DataFrame([np.nan, 1, np.nan, 1, np.nan, 1, np.nan])
    arr = np.array([4, 5, 6])

    i = iter(arr)

    df[0] = np.where(df[0] == 1, next(i), np.nan)

    print(df)

The result is:

     0
0  NaN
1  4.0
2  NaN
3  4.0
4  NaN
5  4.0
6  NaN

But I need the result where consequential numbers from the numpy array are put in the DataFrame like:

     0
0  NaN
1  4.0
2  NaN
3  5.0
4  NaN
5  6.0
6  NaN

I appreciate any help.

CodePudding user response:

it's not the very efficient way but it will do the job.

import pandas as pd
import numpy as np

def util(it, row):
    ele = next(it, None)
    return ele if ele is not None else row

df = pd.DataFrame([np.nan, 1, np.nan, 1, np.nan, 1, np.nan])
arr = np.array([4, 5, 6])
it = iter(arr)

df[0] = np.array(list(map(lambda r : util(it, r) if r == 1.0 else np.nan, df[0])))
  • Related