我正在嘗試旋轉資料框,但它一直回傳 Int64 錯誤。實際上沒有回答類似的問題 -是什么導致這些 Int64 列導致 TypeError?
這是我的資料框的型別:
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 price 30159 non-null Int64
1 type 30159 non-null object
2 size 30155 non-null Int64
3 location 30159 non-null object
4 neighborhood 30159 non-null object
dtypes: Int64(2), object(3)
資料透視表代碼:
pfraw = pd.pivot_table(pfraw, values = 'price', index = 'neighborhood', columns = 'type')
以及錯誤訊息的緯度位:
273 dtype = np.dtype(dtype)
275 if not isinstance(dtype, np.dtype):
276 # enforce our signature annotation
--> 277 raise TypeError(dtype) # pragma: no cover
279 converted = maybe_downcast_numeric(result, dtype, do_round)
280 if converted is not result:
TypeError: Int64
我不明白為什么它會回傳 Int64 錯誤。
uj5u.com熱心網友回復:
首先,讓我們創建一個類似于 OP 的 df
import pandas as pd
df = pd.DataFrame( {'price': [10, 12, 18, 10, 12], 'type': ['A', 'A', 'A', 'B', 'B'], 'size': [10, 12, 18, 10, 12], 'location': ['A', 'A', 'A', 'B', 'B'], 'neighborhood': ['A', 'A', 'A', 'B', 'B']})
如果一個人列印 df 一個人會看到這個有int64和沒有Int64(與 OP 不同)。注意:在我的回答中,發現了兩種 dtype 之間的區別。
print(df.info(verbose=True))
[Out]:
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 price 5 non-null int64
1 type 5 non-null object
2 size 5 non-null int64
3 location 5 non-null object
4 neighborhood 5 non-null object
并且,使用int64一個將能夠創建具有索引“neighborhood”、列“type”和值“price”的資料透視表,具有以下內容
df_pivot = df.pivot_table(index='neighborhood', columns='type', values='price')
這是輸出
type A B
neighborhood
A 13.333333 NaN
B NaN 11.0
但是,使用Int64資料透視表可能會產生錯誤。
為了處理這個問題,需要將型別轉換為int64
df[['price', 'size']] = df[['price', 'size']].astype('int64')
或者
import numpy as np
df[['price', 'size']] = df[['price', 'size']].astype(np.int64)
此外,最有可能的是,OP 有缺失值。處理該問題的最快方法是洗掉具有缺失值的行。為了查找和洗掉缺失值,我在這里的回答可能會有所幫助。
作為參考,這是一個直接鏈接到模塊 maybe_downcast_to_dtype 的鏈接,它引發了 OP 遇到的錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/469035.html
下一篇:使用df.at()編輯多個值
