Home > Software engineering >  Why does pandas DataFrame convert integer to object like this?
Why does pandas DataFrame convert integer to object like this?

Time:11-12

convert to object automatically

I am using value_counts() to get the frequency for sec_id. The output of value_counts() should be integers.

When I build DataFrame with these integers, I found those columns are object dtype. Does anyone know the reason?

CodePudding user response:

They are the object dtype because your sec_id column contains string values (e.g. "94114G"). When you call .values on the dataframe created by .reset_index(), you get two arrays which both contain string objects.

More importantly, I think you are doing some unnecessary work. Try this:

>>> sec_count_df = df['sec_id'].value_counts().rename_axis("sec_id").rename("count").reset_index()
  • Related