0

I'm trying to create a new dataframe from an existing dataframe. I tried groupby but I didn't seem to sum up the strings as a whole number. Instead it returned many strings(colleges in this case).

This is the original dataframe

I tried groupby to get the number(whole number) of the colleges but it returned a many colleges(string) instead

How do I return the number of colleges as an integer in the new column 'totalPlayer'? Please help.

Phong Le
  • 13
  • 4
  • welcome to stack overflow, a website where you should post a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) when you have a problem. pleas look at [this page](https://stackoverflow.com/help/how-to-ask) – warped May 13 '19 at 05:31

1 Answers1

0

Hoping, I understand your question correctly.

You need to count the distinct values in college column.

Assuming df is the name of your data frame. Below code will help.

df['college'].nunique()

Helping Links - Counting unique values in a column in pandas dataframe like in Qlik?

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.nunique.html

Tajinder
  • 2,248
  • 4
  • 33
  • 54
  • I just figured out how to do it. I guess my question was asking for the frequency of colleges but I didn't know how to put it into words. I found out calling .size() will return the number of frequency and turned it into a dataframe by .reset_index(). – Phong Le May 13 '19 at 05:51
  • Ok, please confirm if my answer is not useful. so then I will delete this answer. – Tajinder May 13 '19 at 06:10
  • It is useful! Thank you – Phong Le May 15 '19 at 08:24