回答編集履歴
2
ソートは不要(.loc[lst] で充分)
test
CHANGED
@@ -15,9 +15,8 @@
|
|
15
15
|
|
16
16
|
#
|
17
17
|
lst = ['Foods', 'Entertainment', 'Fixed cost', 'Others', 'Income']
|
18
|
-
sort_order = dict(zip(lst, range(len(lst))))
|
19
18
|
grp = df['category'].str.extract(f'({"|".join(lst)})', expand=False)
|
20
|
-
df2 = df.groupby(grp)['cost'].sum().
|
19
|
+
df2 = df.groupby(grp)['cost'].sum().loc[lst].reset_index()
|
21
20
|
|
22
21
|
print(df2)
|
23
22
|
```
|
1
test
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
> list内の文字を含む行の金額を足し合わせて、list(カテゴリー)ごとの合計金額を出力したい
|
2
|
+
|
2
3
|
```python
|
3
4
|
import random
|
4
5
|
import pandas as pd
|
@@ -12,18 +13,19 @@
|
|
12
13
|
'cost': np.random.randint(100, 5000, N),
|
13
14
|
})
|
14
15
|
|
15
|
-
#
|
16
|
+
#
|
16
17
|
lst = ['Foods', 'Entertainment', 'Fixed cost', 'Others', 'Income']
|
18
|
+
sort_order = dict(zip(lst, range(len(lst))))
|
17
19
|
grp = df['category'].str.extract(f'({"|".join(lst)})', expand=False)
|
18
|
-
df2 = df.groupby(grp)['cost'].sum().reset_index()
|
20
|
+
df2 = df.groupby(grp)['cost'].sum().sort_index(key=lambda i: i.map(sort_order)).reset_index()
|
19
21
|
|
20
22
|
print(df2)
|
21
23
|
```
|
22
24
|
| category | cost |
|
23
25
|
|:-------------:|-------:|
|
26
|
+
| Foods | 23068 |
|
24
|
-
| Entertainment | 5
|
27
|
+
| Entertainment | 35999 |
|
25
|
-
| Fixed cost |
|
28
|
+
| Fixed cost | 28655 |
|
26
|
-
|
|
29
|
+
| Others | 62558 |
|
27
|
-
| Income |
|
30
|
+
| Income | 50058 |
|
28
|
-
| Others | 54547 |
|
29
31
|
|