【解決方法】Typeerror: ufunc ‘isfinite’ は入力タイプに対してサポートされておらず、入力はサポートされているタイプに安全に強制できませんでした


バケットを作成するコードを書いてみました。 バケット化する列には多くの 10 進数値があり、オブジェクト データ型であるため、列を float 型に変換しました。 型変換後、ビンを実行するとエラーが発生します

私が試したこと:

これが私のコードです

“`

Python
import pandas as pd
import numpy as np
df=pd.read_excel(r'D:\Practice Files\Anirudh Exercise Skin India.xlsx')
#print(df)

#Conversion of str to int
df['Basepacksize'] = df['Basepacksize'].astype(str).astype(float)

#Pack Ranges/Sachets/Non-Sachets
bins= ['0','10','15','30','50','100']
df1=pd.cut(df['Basepacksize'], bins=bins)
print(df1)

“`

発生するエラーはこちら

Traceback (most recent call last):
  File "C:\Users\ani\PycharmProjects\pythonProject\main7.py", line 11, in <module>
    df1=pd.cut(df['Basepacksize'], bins)
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 293, in cut
    fac, bins = _bins_to_cuts(
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 444, in _bins_to_cuts
    labels = _format_labels(
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 578, in _format_labels
    precision = _infer_precision(precision, bins)
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 644, in _infer_precision
    levels = [_round_frac(b, precision) for b in bins]
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 644, in <listcomp>    levels = [_round_frac(b, precision) for b in bins]
  File "C:\Users\ani\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\core\reshape\tile.py", line 628, in _round_frac
    if not np.isfinite(x) or x == 0:
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Process finished with exit code 1

解決策 1

これは正しくありません:

Python
bins= ['0','10','15','30','50','100']

ビン値の範囲は 0 から N であるため、次のようになります。

Python
bins= [0, 10, 15, 30, 50, 100]

セットを与える [0 – 10]、 [10 – 15]、 [15 – 30]、 [30 – 50]、 [50 – 100]

見る Python の Pandas.cut() メソッド – GeeksforGeeks[^] サンプルコード用。

コメント

タイトルとURLをコピーしました