pythonpandascsvlog-analysis

How to sum same countries in a csv file using pandas


I have a csv file and there are Count and Country columns. There are many Count and Country columns but this is the example I will write below.

Country     Count     Country     Count 
Japan        654       Japan       566   
 US           90        US          90

And I want the result :

Country     Total Count 
Japan          1220
US              180

How do I add the code in pandas :

import pandas as pd
df = pd.read_csv('/Users/giyan/Desktop/monthly report/geoip/finalsumgeoip.csv')
df['Total Count'] = df.filter(like='Count').sum(axis=1).astype(int)

df = df[['Country','Total Count']]
df.to_csv('podapoda.txt', sep='\t', encoding='utf-8')

Solution

  • You can use the loc method for this. You can replace your your filter line with this:

    df['Total count'] = df.loc[df['A'] =df['C'],['B','D']].sum(axis=1)