简体   繁体   English

将数据框列从 Object 转换为 float 或 int

[英]Convert dataframe column from Object to float or int

I want to convert the dtype of several dataframe columns from the default when read in from CSV:当从 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
  1. trying to convert Total CTC to float :-尝试将Total CTC转换为float :-
DF['Total CTC'] = DF['Total CTC'].astype(float)
ValueError: could not convert string to float: '69,608.00'
  1. trying to convert Total CTC to int :-试图将Total CTC转换为int :-
DF['Total CTC'] = DF['Total CTC'].astype(int)
ValueError: invalid literal for int() with base 10: '69,608.00'
  1. HR['Total CTC'].apply(lambda x:x.dtype)

AttributeError: 'str' object has no attribute 'dtype'

please suggest solution.请提出解决方案。

I want create one new column with multiply this column and another column where value is 5我想创建一个新列,将此列乘以另一列,其中值为 5

您可以像这样转换列:

DF['Total CTC'] = DF['Total CTC'].str.replace(',' ,'').astype(float)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM