0

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?

1
  • Did you check the type of df_pos_0219['sec_id']? Commented Nov 12, 2021 at 2:10

1 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.