将数据格式化(德语)转换为通用python语言

2024-06-10 14:49:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有德语格式数据的数据帧,即通用格式中的{}表示{},而通用格式中的{}表示{}个片段,目前我的python脚本将{}读作字符串,将{}读作{},而在我的理想解决方案中,它应该是{}浮点和{}整数

此外,CSV文件中此24.386999999999997 的正确值为24.387,这实际上意味着24387(整数),但不是整数 他在读另一本书

数据如下


Row    Date    c_s  eV      eC_r    D_O_p   D_Q_p   D_V_E_p 
1   2018-03-01  FR  34.598  1,29    445.0   1.56    24.386999999999997  
2   2018-03-01  DE  159.779 3,01    4.804   24.976  407.38300000000004  
3   2018-03-01  AT  19.878  4,96    985.0   7.703   93.19   
4   2018-03-01  PL  42.387  3,37    1.428   7.478   68.816  

如有任何建议,将不胜感激!谢谢


Tags: 文件csv数据字符串脚本date格式整数
1条回答
网友
1楼 · 发布于 2024-06-10 14:49:29

Pandasread_csv允许您设置千位和十进制分隔符:

df = pd.read_csv('test.csv', delim_whitespace=True, thousands='.', decimal=',', parse_dates=['Date'])

将输出:

^{tb1}$

如果我理解正确的话,如果句点后有超过3位数字,那么之后的所有内容都应该在逗号之后。在这种情况下,您可以编写一个转换器,并将其应用于相应的列:

def process_number(x):
    if len(x.split('.')[-1]) > 3 :
        idx = len(x) - x[::-1].index('.') - 1 #get index number of last occurence of .
        x = x[:idx+4] + ',' + x[idx+4:] #insert comma
        x= x.replace('.','').replace(',','.') #remove periods and replace comma
        return int(round(float(x))) #convert to int
    else:
        return int(x.replace('.',''))
    
converters = {'D_V_E_p': lambda x: process_number(x)}
df = pd.read_csv('test.csv', delim_whitespace=True, thousands='.', decimal=',', parse_dates=['Date'], converters=converters)

输出:

^{tb2}$

相关问题 更多 >