以下のコードはサイトのhtmlをpandasで抽出しdataframeにまとめ,特定の要素を抽出するコードです.要素抽出まではできたのですが,抽出した全要素の型をobject型⇒np.float32に変換したいのですが,AttributeError: module 'pandas' has no attribute 'float32'
と怒られてしまいました.どなたか型変換の方法を教えていただけないでしょうか?
以下,コードと出力結果です.環境はpython3.6 on anacondaです
python
1import pandas as pd 2 3url='http://163.49.30.82/cgi-bin/DspWaterData.exe?KIND=6&ID=303031283301010&BGNDATE=20021201&ENDDATE=20021231&KAWABOU=NO' 4dfs = pd.read_html(url, header=0,index_col=0)#htmlを読み込む 5df = dfs[1] #headerを取り除く 6#print(df) 7 8raw_data = df.values #値を格納 9data = raw_data[1,:] #data.shape is (1,24) 10#data = raw_data[1,:] #要素を抽出 11print(data) 12print('type(data): ', type(data)) 13print('data.dtype: ', data.dtype) 14 15data_float = data(dtype=np.float32) #object -> np.float32に変換 16print(type(data_float))
出力結果は
python
1['222.67' '241.94' '234.14' '226.46' '222.67' '226.46' '222.67' '211.49' 2 '211.49' '209.66' '204.20' '198.82' '186.53' '177.99' '172.97' '166.38' 3 '161.52' '161.52' '164.75' '172.97' '190.00' '195.27' '193.50' '190.00'] 4type(data): <class 'numpy.ndarray'> 5data.dtype: object 6--------------------------------------------------------------------------- 7AttributeError Traceback (most recent call last) 8<ipython-input-39-f78f10ab5754> in <module>() 9 16 print('data.dtype: ', data.dtype) 10 17 11---> 18 data_float = data(dtype=np.float32) #object -> np.float32に変換したい... 12 19 print(type(data_float)) 13 20 14 15AttributeError: module 'pandas' has no attribute 'float32'
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。