【863】Calculate records based on the same value

发布时间 2023-07-20 13:36:38作者: McDelfino

Suppose we have a dataframe, it has a column of "country". It lists different names of country's names, and for one country maybe it has multiple records. Our task is to create a new dataframe which include the country's names and their corresponding counts of records.

m = pd.DataFrame(df_af_mp['country'].value_counts()) 
m = m.rename(columns={'country': 'value'})
m['country'] = m.index 
m.index = range(len(m)) 

m 

Showing the code with seperated steps: