【データ前処理】object型データをfloat型データへ一括変換したい。その際に、文字データが含まれる行を削除した上で行いたい。

以下の内容のオープンデータがあります。CSVファイルです。

ID sex A B C・・・・・・Z
1 M 12 101 51 152
2 F 15 120 23 212
3 F 16 132 20 200
4 M 11 111 22 187


213 M 17 109 21 198

実現したいこと

CSVデータで読み込むと文字データと数字のデータが両方含まれています。
An_Data.info() にて型式を読み込むと全て、objectと認識されます。
An_Data= An_Data.astype('float64') にて、一括でfloat型に変換をしたいです。
しかし、文字データが邪魔になるので、object→float型に変換する過程で文字行を削除するか、SKIPして一括変換を行いたいです。

その後、要約統計量を一括で出力したいのです。

発生している問題・エラーメッセージ


ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2056/3153667598.py in <module>
----> 1 An_Data= An_Data.astype('float64')

~\Anaconda3\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors)
5813 else:
5814 # else, only a single dtype is given
-> 5815 new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
5816 return self._constructor(new_data).finalize(self, method="astype")
5817

~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in astype(self, dtype, copy, errors)
416
417 def astype(self: T, dtype, copy: bool = False, errors: str = "raise") -> T:
--> 418 return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
419
420 def convert(

~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, ignore_failures, **kwargs)
325 applied = b.apply(f, **kwargs)
326 else:
--> 327 applied = getattr(b, f)(**kwargs)
328 except (TypeError, NotImplementedError):
329 if not ignore_failures:

~\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py in astype(self, dtype, copy, errors)
589 values = self.values
590
--> 591 new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
592
593 new_values = maybe_coerce_values(new_values)

~\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_array_safe(values, dtype, copy, errors)
1307
1308 try:
-> 1309 new_values = astype_array(values, dtype, copy=copy)
1310 except (ValueError, TypeError):
1311 # e.g. astype_nansafe can fail on object-dtype of strings

~\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_array(values, dtype, copy)
1255
1256 else:
-> 1257 values = astype_nansafe(values, dtype, copy=copy)
1258
1259 # in pandas we don't store numpy str dtypes, so convert to object

~\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
1093 if arr.ndim > 1:
1094 flat = arr.ravel()
-> 1095 result = astype_nansafe(flat, dtype, copy=copy, skipna=skipna)
1096 # error: Item "ExtensionArray" of "Union[ExtensionArray, ndarray]" has no
1097 # attribute "reshape"

~\Anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna)
1199 if copy or is_object_dtype(arr.dtype) or is_object_dtype(dtype):
1200 # Explicit copy, or required since NumPy can't view from / to object.
-> 1201 return arr.astype(dtype, copy=True)
1202
1203 return arr.astype(dtype, copy=copy)

ValueError: could not convert string to float: 'ID'

該当のソースコード

python

#CSVデータの読み込む(UTF-8文字コードになっているので、日本語使えるASCIIにエンコードする。)An_Data = pd.read_csv('Target.csv', encoding="shift-jis") #データの個数と型を表示する print('★★★ここで、読み込んだデータの個数と型を表示します。')print('★★★non-nullと書いてあれば、空欄はありません。')An_Data= An_Data.astype('float64')An_Data.info() #要約統計量を一括で出力する。 print('★★★以下、要約統計量を一括で出力します。')print('★★★count=データ数 mean=平均値 std=標準偏差 min-max=最小値・25/50/75パーセンタイル値・最大値') An_Data.describe()

コメントを投稿

0 コメント