admin管理员组

文章数量:1201591

I've got a column ['Duration] which is an int datatype. I'm now trying to find out the most occurencing ['Duration'] in a pandas dataframe.

  duration = (inter['duration'].mode())
  print(duration)

Result:

  0    94
  Name: duration, dtype: int64      

The answer is right, but the datatype is wrong. It should be an integer When I ran type function over the duration variable it shows this

  type(duration)

Result:

  pandas.core.series.Series

The variable 'duration' should be an integer and not pandas.series. How can I convert it to that?

I've got a column ['Duration] which is an int datatype. I'm now trying to find out the most occurencing ['Duration'] in a pandas dataframe.

  duration = (inter['duration'].mode())
  print(duration)

Result:

  0    94
  Name: duration, dtype: int64      

The answer is right, but the datatype is wrong. It should be an integer When I ran type function over the duration variable it shows this

  type(duration)

Result:

  pandas.core.series.Series

The variable 'duration' should be an integer and not pandas.series. How can I convert it to that?

Share Improve this question edited Jan 22 at 13:46 jps 22.5k16 gold badges88 silver badges104 bronze badges asked Jan 22 at 13:25 user234568user234568 8154 gold badges14 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There can be more than one value that is the mode.

inter = pd.DataFrame({'duration': [1, 2, 3, 1, 1, 2, 2]})
inter['duration'].mode()

# 0    1
# 1    2
# Name: duration, dtype: int64

If you want to pick the first one, just slice:

inter['duration'].mode().iloc[0]

Example:

inter = pd.DataFrame({'duration': [1, 2, 3, 1, 1, 3, 2]})
inter['duration'].mode().iloc[0]

# 1

Otherwise, you will need to decide what to do with the multiple values (e.g. aggregate them as the mean if this makes sense).

本文标签: pythonHow can I convert this pandasseries result to integerStack Overflow