0

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.

3
  • Do you need it to be efficient or just get the job done on a small dataset? Commented Nov 17, 2022 at 12:18
  • Actual datasets are quite big. Commented Nov 17, 2022 at 12:25
  • If answer below resolve your question please accept it that will close it. Commented Nov 18, 2022 at 5:18

1 Answer 1

2

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])))
Sign up to request clarification or add additional context in comments.

3 Comments

If no more element present in array arr then it will take the value which is present in df in case where df == 1.0 else it will take np.nan if you want to change it in line no 6 instead of returning row return whatever the value you want
I see that such shortened line worked for me df[0] = np.array(map(lambda r : next(it) if r == 1 else np.nan, df[0])) Thank you for the idea!
good to know next(it) might throw exception be aware of that else use next(it, no.nan)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.