Reputation: 2558
I have the following pandas (pd
) dataframe:
> df = pd.DataFrame({'x':[1,2,3], 'y':[4,5,6], 'z':[7,8,9]}, index=['one', 'two', 'three'])
> df
x y z
one 1 4 7
two 2 5 8
three 3 6 9
and a Series:
s = pd.Series([99,99,99])
When I try to assign these values in b
to some column in df
, I don't get any error but all values in that column are set to nan
instead:
> df['y'] = s
> df
x y z
one 1 NaN 7
two 2 NaN 8
three 3 NaN 9
I have set dataframe column values many times before using this assignment technique, why doesn't this work (anymore)?
Upvotes: 0
Views: 2627
Reputation: 26676
ICCU:
Another way of doing it. Please try df.assign
df.assign(y=s.values)
Upvotes: 0
Reputation: 2558
The issue is with the index values in the DataFrame
and Series
: they don't match.
By default, any pandas index has integer values which count up from 0
, so if you don't modify them and the lengths of your column and Series
match, there is no problem.
However, you modified the index values of df
and set them to ['one', 'two', 'three']
.
You should make sure that:
either the Series
uses the same index as the DataFrame
:
> s = pd.Series({'one': 99, 'two': 99, 'three': 99})
> df['y'] = s
or, you can just use the (index-less) values in s
:
> df['y'] = s.values
to obtain:
> df
x y z
one 1 99 7
two 2 99 8
three 3 99 9
Upvotes: 2