Home > other >  Converting class 'pandas.core.series.Series' to "float"
Converting class 'pandas.core.series.Series' to "float"

Time:12-18

I'm doing a training task in Jupyter notebook and I'm writing the following code to count the number of items in the "Trans_id" column of the table:

 df = pd.read_csv('Xyz.csv')
 num_trans = df["trans_id"].value_counts()
 num_trans

However I'm getting the following error:

Your answer has the type <class 'pandas.core.series.Series'> for the num_trans variable but we expected an int or float. Double check your code.

I tried to solve it the following way but it didn't work:

 df = pd.read_csv('Xyz.csv')
 num_trans = df["trans_id"].value_counts()
 num_trans.to_float()
 num_trans

How would you recommend me to solve it? Thank you!!!

CodePudding user response:

EDIT: Misunderstood question earlier.

num_trans is a Series because .value_counts() returns a Series. Refer doc for reference.

However, if I understand correctly, you want to use it outside of pandas' that's why you are trying to make it a float. Instead of float, you should make it a dict !

EDIT2: Based on this comment by OP, they want the count of unique values! So, this is more suitable for the job: num_trans = len(df["trans_id"].unique())

But if you insist on using value_counts, this will also work the same: len(dict(df["trans_id"].value_counts()))

  • Related