Python Forum

Full Version: NaN when creating a new column...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I'm trying to create a new column on a dataframe which counts the number of distinct elements of a variable (VAR2) according to the values of another (VAR1). Like this :
df.groupby('VAR1')['VAR2'].nunique()


It works well whan just printing the result :
print(Type_sup.groupby('VAR1')['VAR2'].nunique())
But as soon as i try to create a new column in my data frame such as :
df['new_variable']=df.groupby('VAR1')['VAR2'].nunique()
i get NaN on every line.


Do you have an idea how to sort it out ?

Thank you by advance :)
(Mar-14-2022, 05:36 PM)menator01 Wrote: [ -> ]This link may help
https://stackoverflow.com/questions/2023...das-series

Sorry but it doesn't help. It doesn't match with my issue.