前提・実現したいこと
pandasで特定の文字列を含む行を抽出したいのですが、
エラーの意味がわからず対処出来なくて困っています。
発生している問題・エラーメッセージ
X R 0 930.502413 5.174416e+01 1 930.515813 3.282915e+02 2 930.528137 7.657924e+02 3 930.545750 1.315058e+03 4 930.572668 2.759820e+03 5 930.608270 4.678862e+03 6 930.649620 6.224117e+03 7 930.688033 8.366338e+03 8 930.721594 1.442890e+04 9 930.759718 1.906799e+04 10 930.798653 1.788987e+04 11 930.845726 1.950268e+04 12 930.900830 1.837961e+04 13 930.953497 2.108790e+04 14 930.992097 2.527069e+04 15 931.033590 3.047089e+04 16 931.083807 3.074064e+04 17 931.143623 6.256868e+05 18 931.190704 1.071830e+05 19 931.218777 1.288520e+04 20 931.247398 3.752056e+04 21 931.292986 1.825358e+05 22 931.358646 2.962152e+05 23 931.432141 3.494768e+05 24 931.519688 1.166834e+05 25 931.609024 1.553704e+06 26 931.681887 1.249848e+06 27 931.756977 1.841589e+05 28 931.849154 1.236554e+05 29 931.937817 7.480655e+06 30 932.013398 9.396562e+05 31 932.097748 4.603418e+05 32 932.174797 1.649318e+05 33 932.236729 5.538161e+05 34 932.307071 2.197064e+05 35 932.393732 1.931978e+05 36 932.495135 1.853218e+05 37 932.617478 2.226389e+05 38 932.732402 1.076801e+05 39 932.808777 6.016562e+04 40 932.861302 4.018710e+05 41 932.917201 3.312529e+05 42 933.002207 1.902758e+05 43 933.114788 4.931995e+05 44 933.229672 5.114198e+05 45 933.319637 2.217866e+05 46 933.429413 1.242082e+06 47 933.563106 3.195172e+05 48 933.696385 3.573644e+05 --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-34-e0d23a93dff8> in <module> 246 print(df) 247 --> 248 df[df['R'].str.contains('0')] ~\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name) 5268 or name in self._accessors 5269 ): -> 5270 return object.__getattribute__(self, name) 5271 else: 5272 if self._info_axis._can_hold_identifiers_and_holds_name(name): ~\anaconda3\lib\site-packages\pandas\core\accessor.py in __get__(self, obj, cls) 185 # we're accessing the attribute of the class, i.e., Dataset.geo 186 return self._accessor --> 187 accessor_obj = self._accessor(obj) 188 # Replace the property with the accessor object. Inspired by: 189 # http://www.pydanny.com/cached-property.html ~\anaconda3\lib\site-packages\pandas\core\strings.py in __init__(self, data) 2039 2040 def __init__(self, data): -> 2041 self._inferred_dtype = self._validate(data) 2042 self._is_categorical = is_categorical_dtype(data) 2043 self._is_string = data.dtype.name == "string" ~\anaconda3\lib\site-packages\pandas\core\strings.py in _validate(data) 2096 2097 if inferred_dtype not in allowed_types: -> 2098 raise AttributeError("Can only use .str accessor with string values!") 2099 return inferred_dtype 2100 AttributeError: Can only use .str accessor with string values!
該当のソースコード
Python
1import math 2import numpy as np 3import pandas as pd 4 5x =np.array([[930.49552541], 6 [930.50241252], 7 [930.50936 ], 8 [930.51581285], 9 [930.52178554], 10 [930.52813743], 11 [930.53590557], 12 [930.54574973], 13 [930.55803645], 14 [930.57266836], 15 [930.58935564], 16 [930.60826951], 17 [930.62884869], 18 [930.64961964], 19 [930.66947954], 20 [930.68803344], 21 [930.70490674], 22 [930.7215936 ], 23 [930.73982617], 24 [930.75971803], 25 [930.77935041], 26 [930.79865324], 27 [930.8207072 ], 28 [930.8457263 ], 29 [930.87278812], 30 [930.90082988], 31 [930.92861418], 32 [930.95349711], 33 [930.97389033], 34 [930.99209669], 35 [931.01197425], 36 [931.03358981], 37 [931.05703237], 38 [931.08380651], 39 [931.11410212], 40 [931.14362348], 41 [931.16924592], 42 [931.19070421], 43 [931.20623112], 44 [931.2187767 ], 45 [931.23142849], 46 [931.24739802], 47 [931.26840248], 48 [931.29298588], 49 [931.32298162], 50 [931.35864627], 51 [931.3950254 ], 52 [931.43214055], 53 [931.47362004], 54 [931.51968795], 55 [931.56676231], 56 [931.60902366], 57 [931.64616018], 58 [931.68188711], 59 [931.71813859], 60 [931.75697725], 61 [931.80058173], 62 [931.84915365], 63 [931.89639139], 64 [931.93781723], 65 [931.97537223], 66 [932.01339803], 67 [932.05415833], 68 [932.09774788], 69 [932.13955831], 70 [932.17479712], 71 [932.20562587], 72 [932.23672897], 73 [932.27051968], 74 [932.30707133], 75 [932.3489253 ], 76 [932.39373234], 77 [932.44158594], 78 [932.49513548], 79 [932.55457704], 80 [932.61747775], 81 [932.67859409], 82 [932.7324024 ], 83 [932.77589621], 84 [932.80877703], 85 [932.8363532 ], 86 [932.86130171], 87 [932.88638003], 88 [932.91720118], 89 [932.95670163], 90 [933.00220653], 91 [933.05387947], 92 [933.11478805], 93 [933.1763619 ], 94 [933.22967197], 95 [933.27447892], 96 [933.31963668], 97 [933.37122972], 98 [933.42941259], 99 [933.49435751], 100 [933.56310613], 101 [933.62940727], 102 [933.69638453], 103 [933.77050269], 104 [933.8519677 ]]) 105 106 107y=np.array([[0.00434942], 108 [0.00434728], 109 [0.00434605], 110 [0.00434515], 111 [0.00434443], 112 [0.00434375], 113 [0.00434299], 114 [0.00434213], 115 [0.00434116], 116 [0.00434013], 117 [0.00433905], 118 [0.00433795], 119 [0.00433684], 120 [0.00433581], 121 [0.00433489], 122 [0.00433407], 123 [0.00433336], 124 [0.00433269], 125 [0.00433198], 126 [0.00433124], 127 [0.00433053], 128 [0.00432986], 129 [0.00432912], 130 [0.00432831], 131 [0.00432747], 132 [0.00432662], 133 [0.00432582], 134 [0.00432511], 135 [0.00432455], 136 [0.00432406], 137 [0.00432354], 138 [0.00432298], 139 [0.00432239], 140 [0.00432172], 141 [0.00432099], 142 [0.0043203 ], 143 [0.0043197 ], 144 [0.00431922], 145 [0.00431887], 146 [0.00431859], 147 [0.00431832], 148 [0.00431797], 149 [0.00431752], 150 [0.004317 ], 151 [0.00431637], 152 [0.00431565], 153 [0.00431492], 154 [0.0043142 ], 155 [0.0043134 ], 156 [0.00431254], 157 [0.00431168], 158 [0.00431093], 159 [0.00431027], 160 [0.00430966], 161 [0.00430904], 162 [0.00430839], 163 [0.00430767], 164 [0.00430688], 165 [0.00430613], 166 [0.00430549], 167 [0.00430491], 168 [0.00430433], 169 [0.00430371], 170 [0.00430307], 171 [0.00430246], 172 [0.00430195], 173 [0.00430151], 174 [0.00430107], 175 [0.00430059], 176 [0.00430009], 177 [0.00429951], 178 [0.0042989 ], 179 [0.00429826], 180 [0.00429755], 181 [0.00429678], 182 [0.00429597], 183 [0.0042952 ], 184 [0.00429452], 185 [0.00429399], 186 [0.00429358], 187 [0.00429325], 188 [0.00429295], 189 [0.00429265], 190 [0.00429228], 191 [0.00429181], 192 [0.00429127], 193 [0.00429067], 194 [0.00428997], 195 [0.00428927], 196 [0.00428867], 197 [0.00428817], 198 [0.00428767], 199 [0.00428711], 200 [0.00428648], 201 [0.00428578], 202 [0.00428505], 203 [0.00428436], 204 [0.00428366], 205 [0.0042829 ], 206 [0.00428208]]) 207 208 209def get_circle_radius(x1, y1, x2, y2, x3, y3): 210 d = 2 * ((y1 - y3) * (x1 - x2) - (y1 - y2) * (x1 - x3)) 211 x = ((y1 - y3) * (y1 ** 2 - y2 ** 2 + x1 ** 2 - x2 ** 2) - (y1 - y2) * (y1 ** 2 - y3 ** 2 + x1 ** 2 - x3 ** 2)) / d 212 y = ((x1 - x3) * (x1 ** 2 - x2 ** 2 + y1 ** 2 - y2 ** 2) - (x1 - x2) * (x1 ** 2 - x3 ** 2 + y1 ** 2 - y3 ** 2)) / -d 213 r = math.sqrt((x - x1) ** 2 + (y - y1) ** 2) 214 return r 215 216i = 0 217row_num = i 218R = [] 219X_center = [] 220Y2 = [] 221while i <= 96 : 222 x = np.ravel(x).tolist() 223 y = np.ravel(y).tolist() 224 X = x[row_num] 225 Y = y[row_num] 226 227 i = i + 1 228 row_num = i 229 X2 = x[row_num] 230 X_center.append(X2) 231 Y2 = y[row_num] 232 233 i = i + 1 234 row_num = i 235 X3 = x[row_num] 236 Y3 = y[row_num] 237 238 R.append(get_circle_radius(X,Y,X2,Y2,X3,Y3)) 239 240XYR = np.concatenate([np.array(X_center).reshape(-1, 1), np.array(R).reshape(-1, 1)], 1) 241 242df = pd.DataFrame(XYR) 243 244df.columns = ['X', 'R'] 245 246print(df) 247 248df[df['R'].str.contains('0')]
補足情報(FW/ツールのバージョンなど)
windows 64bit
Anaconda Jupyter Notebook
回答2件
あなたの回答
tips
プレビュー