1

I'm trying to replace all NaN values in a dataframe with an array [NaN, NaN, NaN] instead. But the normal replace methods (df.replace) don't work because to_replace and value are not the same size.

Thanks in advance :)

0

2 Answers 2

0

Recomend you Read: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html

Also you Must See this: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.fillna.html#pandas.DataFrame.fillna

If this Help let me Know Please, I'm New on Stackoverflow and i'll try to Help When i See Something that i Can Help. Thanks.

Sign up to request clarification or add additional context in comments.

1 Comment

It was very helpful! Dived into df.fillna and solved the problem. Thanks
0

I figured something, that should work:

import pandas as pd
import numpy as np

# Sample df with a NaN in column "y"
df = pd.DataFrame({"y": np.NaN, "x": ["Some Value"]})

# Get the nans
isna = df['y'].isna()

# Replace with series with list of (3) nans
df.loc[isna, 'y'] = pd.Series([[np.NaN, np.NaN, np.NaN]] * isna.sum()).values

print(df)

Mainly taken from: https://stackoverflow.com/a/61944174/5635941

Comments

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.