Mar-14-2022, 05:32 PM
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 :
It works well whan just printing the result :
Do you have an idea how to sort it out ?
Thank you by advance :)
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 :)