慕工程0101907
2022-12-20 15:02:20
在 python3 和 pandas 中,我像這樣打開一個(gè) 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'有識(shí)別碼,有時(shí)只能有數(shù)字或數(shù)字和字母在這種情況下,該行的代碼在 CSV 中為“2,29E+13”。這是一個(gè)公司代碼,我知道是“22896431000110”請(qǐng)問,如何將列符號(hào)轉(zhuǎn)換為整數(shù)?我想將指數(shù)表示法值轉(zhuǎn)換為 int。同時(shí),我將檢查 CSV 值是否正確
1 回答

慕哥6287543
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
試試這個(gè) -
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
添加回答
舉報(bào)
0/150
提交
取消