回答編集履歴
2
Update
test
CHANGED
@@ -13,15 +13,16 @@
|
|
13
13
|
|
14
14
|
#
|
15
15
|
cols = ['a', 'b', 'c']
|
16
|
+
tags = ['pre', 'post']
|
17
|
+
col_prefix = [{c: f'{t}_' + c for c in cols} for t in tags]
|
16
18
|
result = (
|
17
|
-
df.groupby(['day', 'id'], sort=False)
|
19
|
+
df.groupby(['day', 'id'], sort=False)
|
18
|
-
.apply(lambda x: pd.concat(
|
20
|
+
.apply(lambda x: pd.concat(
|
19
|
-
x.loc[x['pre_post']==
|
21
|
+
x.loc[x['pre_post'] == t, cols].iloc[0].rename(col_prefix[i])
|
20
|
-
|
22
|
+
for i, t in enumerate(tags)))
|
21
|
-
.reset_index())
|
23
|
+
.reset_index())
|
22
24
|
|
23
25
|
print(result)
|
24
|
-
|
25
26
|
#
|
26
27
|
day id pre_a pre_b pre_c post_a post_b post_c
|
27
28
|
0 2022-01-01 00:00:00 1234 1 2 3 4 5 6
|
1
Update
test
CHANGED
@@ -9,9 +9,9 @@
|
|
9
9
|
2022-01-15 00:00:00,5678,pre,7,8,9
|
10
10
|
2022-01-15 00:00:00,5678,post,10,11,12
|
11
11
|
'''
|
12
|
-
|
13
12
|
df = pd.read_csv(io.StringIO(csv_data))
|
14
13
|
|
14
|
+
#
|
15
15
|
cols = ['a', 'b', 'c']
|
16
16
|
result = (
|
17
17
|
df.groupby(['day', 'id'], sort=False)
|