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
>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()