簡體   English   中英

在數據框中轉換為長度為20的數字數據

[英]Cast to numeric data of length 20 in dataframe

我的csv文件具有幾個長度為20的數字數據。當我在數據幀中讀取它時,它被讀取為dtype對象。 我需要將所有數字數據轉換為Integer。

我的數據是csv看起來像:

emp_id,age,salary,marital
21012334509821345944,22,4500,married
21012334509821345945,22,4510,single
21012334509821345946,22,45040,married
21012334509821345947,22,41500,single
21012334509821345948,22,54500,single
21012334509821345949,22,64500,married

我試過了 :

d1 = pd.read_csv('D:\\Exercise\\test.csv')
d1.set_index('emp_id',inplace = True)
d1.index = d1.index.map(int) #OverflowError: int too big to convert
print(d1.index.values)

如果我評論索引圖,則會得到類似以下的輸出:['21012334509821345944''21012334509821345945''21012334509821345946''21012334509821345947''21012334509821345948''21012334509821345949']

但是我需要整數。 我嘗試單獨鑄造第一列。 如果具有數值,是否可以將數據框中的所有數據強制轉換。 我嘗試使用numpy轉換。我遇到同樣的錯誤。 謝謝。

可以由整數(np.uint64)表示的最大值是18446744073709551615。因此,可能您將無法做到這一點。

Pandas / Numpy將整數保持為64位。 也許更大,但是重點是有限的。 您需要將它們存儲為dtype object但其值應為int

這是一種方法:

df.emp_id.values[:] = [*map(int, df.emp_id)]

然后,您可以進行數學運算。

df.emp_id // int(1e10)

0    2101233450
1    2101233450
2    2101233450
3    2101233450
4    2101233450
5    2101233450
Name: emp_id, dtype: object

不會對數學進行優化,但是應該可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM