'How to merge and concat in for loop in python
I have a following data
list = ['AA','BB','CC']
| Date | AA | BB | CC | XYZ |
|---|---|---|---|---|
| 20220101 | True | True | True | 100 |
| 20220101 | False | False | True | 100 |
| 20220102 | False | True | True | 100 |
| 20220103 | False | True | True | 600 |
| 20220103 | True | True | False | 400 |
| 20220104 | True | True | True | 100 |
| 20220105 | False | False | True | 200 |
| 20220106 | True | True | False | 100 |
I need the following output:
| Date | AA | BB | CC |
|---|---|---|---|
| 20220101 | 100 | 100 | 200 |
| 20220102 | 0 | 100 | 100 |
| 20220103 | 400 | 1000 | 600 |
| 20220104 | 100 | 100 | 100 |
| 20220105 | 100 | 0 | 200 |
| 20220106 | 100 | 100 | 0 |
I tried
dp = ['AA','BB','CC']
kf = pd.DataFrame()
for i in dp:
sd = df[(df[i] == True)].groupby(['Date'])['XYZ'].sum().reset_index().rename(columns={'XYZ': i})
new = pd.concat([kf, sd], ignore_index = True)
but its not working and only getting last 'CC' in table.
Solution 1:[1]
IIUC, use for a new DataFrame:
l = ['AA', 'BB', 'CC']
df2 = df.copy()
df2[l] = df[l].mul(df['XYZ'], axis=0)
df2 = df2.drop('XYZ', axis=1).groupby('Date').sum()
output:
Date AA BB CC
0 20220101 100 100 200
1 20220102 0 100 100
2 20220103 400 1000 600
3 20220104 100 100 100
4 20220105 0 0 200
5 20220106 100 100 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
