回答編集履歴
4
test
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
```python
|
2
|
-
dfx = df.query('K in (1, 2, 3)')
|
3
|
-
dfx = df
|
2
|
+
dfx = df.groupby(['date1', 'date2'])['K']\
|
4
|
-
|
3
|
+
.agg(M12 = lambda k: df.loc[k.index[k.isin((1, 2))], 'V'].sum(),
|
5
|
-
|
4
|
+
M3 = lambda k: df.loc[k.index[k.eq(3)], 'V'].sum())\
|
6
|
-
|
5
|
+
.reset_index()
|
7
6
|
|
8
7
|
print(dfx)
|
9
8
|
```
|
3
test
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
```python
|
2
2
|
dfx = df.query('K in (1, 2, 3)')
|
3
|
-
dfx = dfx.groupby(['date1', 'date2'])\
|
3
|
+
dfx = dfx.groupby(['date1', 'date2'])['K']\
|
4
|
-
.agg(M12 =
|
4
|
+
.agg(M12 = lambda k: dfx.loc[k.index[k.ne(3)], 'V'].sum(),
|
5
|
-
M3 =
|
5
|
+
M3 = lambda k: dfx.loc[k.index[k.eq(3)], 'V'].sum())\
|
6
6
|
.reset_index()
|
7
7
|
|
8
8
|
print(dfx)
|
2
test
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
```python
|
2
2
|
dfx = df.query('K in (1, 2, 3)')
|
3
|
+
dfx = dfx.groupby(['date1', 'date2'])\
|
4
|
+
.agg(M12 = ('K', lambda x: dfx.loc[x.index[x.ne(3)], 'V'].sum()),
|
3
|
-
|
5
|
+
M3 = ('K', lambda x: dfx.loc[x.index[x.eq(3)], 'V'].sum()))\
|
4
|
-
.set_axis(['M12', 'M3'], axis=1).fillna(0, downcast='infer')\
|
5
|
-
.reset_index()
|
6
|
+
.reset_index()
|
6
7
|
|
7
8
|
print(dfx)
|
8
9
|
```
|
1
test
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
```python
|
2
|
+
dfx = df.query('K in (1, 2, 3)')
|
2
|
-
dfx = df.groupby(['date1', 'date2', df['K'].eq(3)])['V'].sum().unstack()\
|
3
|
+
dfx = dfx.groupby(['date1', 'date2', df['K'].eq(3)])['V'].sum().unstack()\
|
3
|
-
.set_axis(['M12', 'M3'], axis=1).fillna(0, downcast='infer')\
|
4
|
+
.set_axis(['M12', 'M3'], axis=1).fillna(0, downcast='infer')\
|
4
|
-
.reset_index().rename_axis(None, axis=1)
|
5
|
+
.reset_index().rename_axis(None, axis=1)
|
5
6
|
|
6
7
|
print(dfx)
|
7
8
|
```
|