Leon
Leon

Reputation: 112

Why does pandas DataFrame convert integer to object like this?

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?

Upvotes: 0

Views: 1339

Answers (1)

Andrew Eckart
Andrew Eckart

Reputation: 1728

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

Upvotes: 3

Related Questions