Python Forum
NaN when creating a new column...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NaN when creating a new column...
#1
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 :)
Reply
#2
This link may help
https://stackoverflow.com/questions/2023...das-series
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,155 Apr-19-2022, 07:36 AM
Last Post: JulianZ
Photo Creating column in python based on the other colums value count Bartek635 2 2,965 Apr-15-2021, 03:47 PM
Last Post: Bartek635
  Creating new column with a input string drunkenneo 2 2,283 Apr-14-2021, 08:10 AM
Last Post: drunkenneo
  Creating a new DataFrame from another DataFrame column Ilangos 1 2,080 Jun-05-2019, 09:47 AM
Last Post: Ilangos

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020