-1

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!!!

001
  • 13,291
  • 5
  • 35
  • 66
  • 1
    [How do I get the row count of a Pandas DataFrame?](https://stackoverflow.com/a/15943975) – 001 Dec 15 '22 at 18:36
  • 1
    It isn't clear what you are expecting, `.value_counts()` returns another *series*. How exactly should a series, say, `pd.Series([1, 2, 3]).value_counts()` be converted to a `float`? – juanpa.arrivillaga Dec 15 '22 at 18:53
  • Welcome to Stack Overflow! I'm voting to close the question because it's not clear what you're looking for. It sounds like you want either the len of `df["trans_id"]` or the number of non-null items in it, but I'm not sure why the system would be expecting a float for that. You can [edit] to clarify. Please make a [mre], or as close as possible. – wjandrea Dec 15 '22 at 21:31
  • 1
    It seems, indeed that I need the len of df["trans_id"], as I need to count the number of transactions in the table for marketing research and the number of transactions is in the end the number of rows (this is a cleaned sample and it doesn´t contain duplicate values). I first wanted to count only unique values that is why I tried value_counts and applied it to the column 'trans_id' with id numbers of transactions. – Anna Matinyan Dec 16 '22 at 01:00

1 Answers1

-1

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()))

Zircoz
  • 504
  • 3
  • 14
  • 1
    this doesn't actually create any `int` or `float` objects. The OP seems to be using some automated grader, and it expects `num_trans` variable to have an object of the type `int` or `float` – juanpa.arrivillaga Dec 15 '22 at 19:06
  • Well, OP gotta tell us what they want to do with value_counts()'s output now, lol. – Zircoz Dec 15 '22 at 19:10