Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a pd.Series (with zero NaNs) to a pd.DataFrame is resulting in some NaNs in the added column

When I add a series that has zero NaNs to a pandas DataFrame, the column added is filled with some NaNs.
pd.Series that I am trying to add
When I add it to a pandas DataFrame, the result is like:
Before the addition of column
After the addition of column
The length of both dataframe and series are same.

Why is this the case, and how to resolve this issue?

I tried to use .loc instead of regular indexing but still the problem is not resolved. I would like to know a way to add the new column into the existing dataframe without any NaNs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Reason for missing values are different index values in Series and Dataframe, for prevent it convert Series to numpy array:

df['fruitsNseeds'] = transformed.to_numpy()

If train_x is Series first convert it to DataFrame:

df = train_x.to_frame('col')
df['new'] = transformed.to_numpy()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading