简体   繁体   中英

value_counts() shows counts from another pandas data frame

I'm brand new with python and I'm playing around with data related to cars. I've copied and reset index for new df

stelvio=pd.DataFrame(cars).copy()
stelvio.drop(stelvio[stelvio["model"]!="Stelvio"].index, inplace=True)
stelvio.reset_index(drop=True, inplace=True)

and wanted to check color count so I've run:

stelvio.color.value_counts()

Red           42
Grey          36
Black         36
White         30
Blue          21
Other          8
Green          5
Silver         3
Brown          3
Yellow         0
Gold           0
Purple         0

and

stelvio.info()

gives

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 184 entries, 0 to 183

There are now yellow or gold color cars in stelvio df, so why I see them on value_counts() result for new df?

Seems like this comes from the fact that your data is seen as 'categorical' instead of strings - Why does pandas value_counts() show a count of zero for some values? - explained here.

If that is the case, then you can do

stelvio.color.astype(str).value_counts()

and your '0 rows' should be gone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM