0

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')
Angeline
  • 109
  • 10

1 Answers1

0

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)
Keiku
  • 8,205
  • 4
  • 41
  • 44
DZ1
  • 64
  • 1