[英]Convert dataframe column from Object to float or int
當從 CSV 讀入時,我想從默認值轉換幾個數據框列的 dtype:
DF = pd.read_csv('data.csv', encoding = 'ISO-8859-1',
usecols = ['Email ID','Total CTC'], dayfirst = True, parse_dates=True)
DF.dtypes
Email ID object
GDOJ object
Total CTC object
dtype: object
Total CTC
轉換為float
:-DF['Total CTC'] = DF['Total CTC'].astype(float)
ValueError: could not convert string to float: '69,608.00'
Total CTC
轉換為int
:-DF['Total CTC'] = DF['Total CTC'].astype(int)
ValueError: invalid literal for int() with base 10: '69,608.00'
HR['Total CTC'].apply(lambda x:x.dtype)
AttributeError: 'str' object has no attribute 'dtype'
請提出解決方案。
我想創建一個新列,將此列乘以另一列,其中值為 5
您可以像這樣轉換列:
DF['Total CTC'] = DF['Total CTC'].str.replace(',' ,'').astype(float)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.