为了账号安全,请及时绑定邮箱和手机立即绑定

在熊猫中如何将符号转换为整数?

在熊猫中如何将符号转换为整数?

慕工程0101907 2022-12-20 15:02:20
在 python3 和 pandas 中,我像这样打开一个 CSV:import pandas as pdkwargs = {'sep': ';', 'dtype': str, 'encoding': 'utf-8'}df = pd.read_csv("COVID_sp_29_mai_2020.csv", **kwargs)df.info()<class 'pandas.core.frame.DataFrame'>RangeIndex: 7119 entries, 0 to 7118Data columns (total 27 columns): #   Column                          Non-Null Count  Dtype ---  ------                          --------------  -----  0   Secretaria                      7119 non-null   object 1   Assunto                         7119 non-null   object 2   13979                           7119 non-null   object 3   Valor de Empenho no Processo    7119 non-null   object 4   Órgão                           7119 non-null   object 5   Código UG Documento             7119 non-null   object 6   Descrição Processo              7119 non-null   object 7   Finalidade                      7119 non-null   object 8   R$ Empenho                      7119 non-null   object 9   Tipo Documento                  7119 non-null   object 10  Data Atualização                7119 non-null   object 11  R$ Pago                         7119 non-null   object 12  Número Empenho                  7119 non-null   object 13  Número Documento NE Ref / Anul  335 non-null    object 14  Número Processo                 7119 non-null   object 15  CGC CPF                         7026 non-null   object 16  Nome Credor                     7119 non-null   object 17  Data Emissão                    7119 non-null   object 18  Ação Orçamentária               7119 non-null   object 19  Fonte                           7119 non-null   object 20  Código Fonte                    7119 non-null   object 21  Descrição                       7119 non-null   object列有'CGC CPF'有识别码,有时只能有数字或数字和字母在这种情况下,该行的代码在 CSV 中为“2,29E+13”。这是一个公司代码,我知道是“22896431000110”请问,如何将列符号转换为整数?我想将指数表示法值转换为 int。同时,我将检查 CSV 值是否正确
查看完整描述

1 回答

?
慕哥6287543

TA贡献1831条经验 获得超10个赞

试试这个 -


df['CGC CPF'].fillna('UNK', inplace=True)

df.loc[:, 'CGC CPF'] = df.loc[:, 'CGC CPF'].str.replace(',','') #replacing ',' in target column with blank, for us to interpret exponential values

match = ['E+','e+'] #Since there are both e+ and E+ notation

df['CGC CPF'] = df['CGC CPF'].apply(lambda x: float(x) if any(m in x for m in match) else x)  #using float to convert exponential to a number



查看完整回答
反对 回复 2022-12-20
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号