Python Beginner: Add only ACC_CNT whose COUNTRY column is CN

Asked 2 years ago, Updated 2 years ago, 99 views

Beginner asks the master a question.

When there is a table like below, What should I do when I want to add only ACC_CNT of CN in the COUNTRY column? I'd appreciate it if you could answer me!

python pandas group-by

2022-09-20 20:00

1 Answers

import numpy asp
import pandas as pd

np.random.seed(100)
df = pd.DataFrame(np.random.choice([11,220,55,52,44], size=(5,5)), columns=list('ABCDE'))
print (df)
df.loc[df['C'] == 11 , 'E'] += 1000
#C column to E column for row 11 += 1000
print (df)

'''
     A   B   C   D    E
0   11  11  52  11   55
1   44  55  55  55   55
2  220  11  11  44   52
3   44  55  11  52  220
4   55  52  44  44  220
     A   B   C   D     E
0   11  11  52  11    55
1   44  55  55  55    55
2  220  11  11  44  1052
3   44  55  11  52  1220
4   55  52  44  44   220
'''

Next time, when you post a question, please send us the source code.

I can't check if you use Pandas or not.


2022-09-20 20:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.