Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Value_Counts Question
#4
You cant use your value_counts() serie to index your dataframe - value_counts returns serie with length equal to a number of unique items in column "title", different from length of original dataframe.

You need to use something more complicated, like use value counts to get unique items (counted items form index of value_counts serie) and after that use dataframe selection with .isin() function.

Simple example with fictional data;
In [27]: df = pd.DataFrame({'title': ['steward', 'chef', 'cook', 'steward', 'cook'], 'value':[13,34,23,30,17]})

In [28]: counts = df.title.value_counts()

In [29]: counts
Out[29]: 
steward    2
cook       2
chef       1
Name: title, dtype: int64

In [30]: counts[counts==1]
Out[30]: 
chef    1
Name: title, dtype: int64

In [31]: df[df.title.isin(counts[counts==1].index)]
Out[31]: 
  title  value
1  chef     34
Reply


Messages In This Thread
Value_Counts Question - by smw10c - Mar-28-2017, 02:40 PM
RE: Value_Counts Question - by nilamo - Mar-28-2017, 02:54 PM
RE: Value_Counts Question - by smw10c - Mar-28-2017, 03:16 PM
RE: Value_Counts Question - by zivoni - Mar-28-2017, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts Kristenl2784 4 7,400 Jul-17-2020, 01:50 AM
Last Post: palladium

Forum Jump:

User Panel Messages

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