回答編集履歴
1
補足を追加
test
CHANGED
@@ -31,3 +31,99 @@
|
|
31
31
|
|
32
32
|
|
33
33
|
で、上記のエラーを修正して、コードを実行したところ、**理想** とされる結果が得られているようですが、一つ目の質問の現象は再現されていないように思います。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
**【補足】**
|
44
|
+
|
45
|
+
あと質問にあるコードは、全体的に 行と列の指定が位置データによる指定(``iloc[]``)に偏っているように見えます。
|
46
|
+
|
47
|
+
以下のように Index NameやColumn Name による指定``loc[]``を使うともう少しシンプルにかけるのではないでしょうか。
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
```Python
|
52
|
+
|
53
|
+
import pandas as pd
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
df1=pd.DataFrame([[45,1,2,0,2,2],
|
58
|
+
|
59
|
+
[45,2,3,0,None,None],
|
60
|
+
|
61
|
+
[45,3,4,10,5,5],
|
62
|
+
|
63
|
+
[45,3,6,358,3,4],
|
64
|
+
|
65
|
+
[45,4,5,0,2,2],
|
66
|
+
|
67
|
+
[45,5,6,0,3,None],
|
68
|
+
|
69
|
+
[45,7,8,0,5,5],
|
70
|
+
|
71
|
+
[35,2,3,10,None,2],
|
72
|
+
|
73
|
+
[35,3,4,10,None,None],
|
74
|
+
|
75
|
+
[35,4,5,10,5,6],
|
76
|
+
|
77
|
+
[35,5,7,20,2,2],
|
78
|
+
|
79
|
+
[35,6,7,20,None,None],
|
80
|
+
|
81
|
+
[35,7,8,15,5,5],
|
82
|
+
|
83
|
+
[25,1,2,6,None,5]],
|
84
|
+
|
85
|
+
columns=["a","b","c","d","e","f"])
|
86
|
+
|
87
|
+
df=df1.copy()
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
for col in df.loc[:, 'e':]:
|
92
|
+
|
93
|
+
for idx, row in df[df[col].isnull()].iterrows():
|
94
|
+
|
95
|
+
m = df[(df['a'] == row['a']) & (df['c'] == row['b'])]
|
96
|
+
|
97
|
+
if m.empty:
|
98
|
+
|
99
|
+
continue
|
100
|
+
|
101
|
+
elif len(m) == 1:
|
102
|
+
|
103
|
+
mae = m.iloc[0]
|
104
|
+
|
105
|
+
else:
|
106
|
+
|
107
|
+
mae = m.loc[m.apply(lambda r: min(abs(r['d']-row['d']),360-r['d']+row['d']) , axis=1).idxmin()]
|
108
|
+
|
109
|
+
u = df[(df['a']==df.loc[idx,'a']) & (df['b']==df.loc[idx,'c'])]
|
110
|
+
|
111
|
+
if u.empty:
|
112
|
+
|
113
|
+
continue
|
114
|
+
|
115
|
+
elif len(u) == 1:
|
116
|
+
|
117
|
+
ushiro = u.iloc[0]
|
118
|
+
|
119
|
+
else:
|
120
|
+
|
121
|
+
ushiro = u.loc[u.apply(lambda r: min(abs(r['d']-row['d']),360-r['d']+row['d']) , axis=1).idxmin()]
|
122
|
+
|
123
|
+
df.at[idx,col] = 2 / (1 / mae[col] +1 / ushiro[col])
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
print(df)
|
128
|
+
|
129
|
+
```
|