1

I need to fill a certain value of null based on mode. I thought by groupby() the name and the engine column by mode would solve the problem.

engine_df = df.groupby(['Name']).agg({'Engine':pd.Series.mode})[['Engine']].reset_index()

output:

Name Chevrolet Cruze LTZ AT
Engine [1991.0, 1998.0]
Name: 176, dtype: object

Because they have the same quantity, they appear with 2 values, while I need only 1 value. How to select the first value only?

martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

You could use the first() method.

engine_df = df.groupby(['Name']).agg({'Engine':pd.Series.mode})[['Engine']].first().reset_index()

Here is the documentation on this function. This post has a lot of nice examples of its usage.