回答編集履歴
2
サンプル追加
test
CHANGED
@@ -21,3 +21,115 @@
|
|
21
21
|
|
22
22
|
|
23
23
|
を行っております。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
---
|
28
|
+
|
29
|
+
**動作サンプルコード**
|
30
|
+
|
31
|
+
```Python
|
32
|
+
|
33
|
+
import pandas as pd
|
34
|
+
|
35
|
+
import io
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
data_csv = """
|
40
|
+
|
41
|
+
id,id2,cost
|
42
|
+
|
43
|
+
1,10,5
|
44
|
+
|
45
|
+
2,10,3
|
46
|
+
|
47
|
+
3,10,1
|
48
|
+
|
49
|
+
1,13,10
|
50
|
+
|
51
|
+
2,13,5
|
52
|
+
|
53
|
+
3,13,4
|
54
|
+
|
55
|
+
"""
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
df = pd.read_csv(io.StringIO(data_csv))
|
60
|
+
|
61
|
+
print(df)
|
62
|
+
|
63
|
+
# id id2 cost
|
64
|
+
|
65
|
+
#0 1 10 5
|
66
|
+
|
67
|
+
#1 2 10 3
|
68
|
+
|
69
|
+
#2 3 10 1
|
70
|
+
|
71
|
+
#3 1 13 10
|
72
|
+
|
73
|
+
#4 2 13 5
|
74
|
+
|
75
|
+
#5 3 13 4
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
conv = df.groupby('id2')['cost'].min()
|
80
|
+
|
81
|
+
print(conv)
|
82
|
+
|
83
|
+
#id2
|
84
|
+
|
85
|
+
#10 1
|
86
|
+
|
87
|
+
#13 4
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
list_csv = """
|
92
|
+
|
93
|
+
id2,min
|
94
|
+
|
95
|
+
10,
|
96
|
+
|
97
|
+
13,
|
98
|
+
|
99
|
+
"""
|
100
|
+
|
101
|
+
df1 = pd.read_csv(io.StringIO(list_csv))
|
102
|
+
|
103
|
+
print(df1)
|
104
|
+
|
105
|
+
# id2 min
|
106
|
+
|
107
|
+
#0 10 NaN
|
108
|
+
|
109
|
+
#1 13 NaN
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
ret = df1['id2'].map(conv)
|
114
|
+
|
115
|
+
print(ret)
|
116
|
+
|
117
|
+
#0 1
|
118
|
+
|
119
|
+
#1 4
|
120
|
+
|
121
|
+
#Name: id2, dtype: int64
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
df1['min'] = ret
|
126
|
+
|
127
|
+
print(df1)
|
128
|
+
|
129
|
+
# id2 min
|
130
|
+
|
131
|
+
#0 10 1
|
132
|
+
|
133
|
+
#1 13 4
|
134
|
+
|
135
|
+
```
|
1
説明追加
test
CHANGED
@@ -5,3 +5,19 @@
|
|
5
5
|
df1['min'] = df1['id2'].map(df.groupby('id2')['cost'].min())
|
6
6
|
|
7
7
|
```
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
少し説明を加えると
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
1. ``df.groupby('id2')['cost'].min()`` にて df(``data.csv``)のデータフレームを ``id2``の値ごとに``cost``列が最小の値を導く
|
16
|
+
|
17
|
+
2. ``df1['id2'].map(...)`` にて上記の結果を df1(``list.csv``)の``id2``列の値で引き当てる
|
18
|
+
|
19
|
+
3. 結果をdf1の``min``列に格納する
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
を行っております。
|