PythonRead_excel包含混合的十进制逗号或点和整数的值



(我还没有找到一个解决方案来解决我的"组合"例完全)在阅读答案/解决方案时,我遇到了一些问题,我仍然面临以下障碍。显然,这并不是"手动"清理一些文件会更快的问题。而是多个excel格式的文件流,Python脚本似乎(将)是一个完美的工具。

excel格式文件,我得到有数字在某些列(如"单位销售价格";或"销售金额"),由MS Excel存储并显示为"一般";格式。这给了一个奇特的结果,即使在Excel本身,因为那些与小数点符号显示为字符串/文本(向左调整),而"整数"字符串,即。没有任何小数部分或符号显示为同一列的数字(向右调整)。但外表并不重要。是的,这是一种混合:基本上行应该带一个小数逗号","但有些行-在同一个文件-可能有一个小数点-这是一个错误,我的系统,这就是为什么我试图用Python脚本清理它。最后,我可以管理任何十进制符号(逗号或点),只要它在特定列/列的所有文件中是统一的

小数点逗号和/或点是要管理的事情之一,我已经尝试了一些工作解决方案,也在stackoverflow(谢谢!)中提供,如在这里:逗号作为小数分隔符在read_excel for Pandas

但也有一些行(如。单元格)包含的值实际上是一个整数(毫无疑问,有些价格可能像USD 100而不含美分,对吧?)然后,如果它们在Excel中显示为(即)100,而不是100,00或100.00,我就会失去这些值。

问题1。Python不能"pd.read_excel"值并将它们直接重新格式化为float(),而不需要我告诉它们可能有小数点或小数逗号(.astype('float')或float()会喜欢在默认情况下只有小数点)

问题2。在解决问题1时。我无法使脚本足够智能,以正确地将那些实际上是没有任何符号或小数部分的整数重新格式化为float()。

问题3。如果我是"pd.excel_read"-直接使用excel并获取"整数"正确读取(这允许避免问题2),那么我就没有机会告诉pd.excel_read()函数,它应该读取逗号","作为小数点。这是因为pd.read_excel("file.xlsx", decimal=',') -抛出一个错误,表示'decimal"对于pd.read_excel()来说是未知的。多次检查拼写错误等,我…

"Conversions"函数方法适用于逗号/点问题,除了所有带有"string "的单元格。与INT等价,即没有任何小数部分或任何符号的纯整型数字,将简单地返回为nulls/disappear。

我在论坛上发现的这些问题几年前就已经存在了,但是,没有一个能一次性解决所有问题的解决方案。今天是2023年1月2日,我的熊猫版本是1.3.4。非常感谢"组合"。建议以上几点。现在看到的唯一方法是更详细的字符串上的区域方法,但我感觉我错过了一些更合适的解决方案。

小数逗号和/或点是需要管理的事情之一,我已经尝试了一些工作解决方案,也在stackoverflow(谢谢!)中提供:在read_excel for Pandas中,逗号作为小数分隔符但是"类整数"字符串/对象(当Python读取其类型时)没有正确转换为浮点数,实际上会丢失为null。

我已经提出了这样一个解决方案,但希望可以提出更简单的方案:

# df=pd.read_excel("file.xlsx", decimal=',') # <<< my pandas does NOT recognize decimal=',' as a valid option/argument

df=pd.read_excel("file.xlsx")
for a_column in columns_to_have_fomat_changed:# <<< that is to avoid wasting time for processing columns of no importance. My Excel files come with 150+ columns.
# df[a_column] = df[a_column].astype('float')# <<< here will be errors since comma instead of a point may happen
# df[a_column] = df[a_column].str.replace(",", ".").astype(float) # <<< here all "integers" will be lost


for i in range(len(df[a_column]-1)): # <<< this is for distinguishing from the "integer" strings
if r"," in df[a_column][i] or r"." in df[a_column][i]:
df[a_column][i] = pd.to_numeric(df[a_column][i].str.replace(',', '.').str.replace(' ', ''), # <<< overstack solution working for mixed decimal signs etc.
errors='coerce')
else:
df[a_column][i]=df[a_column][i].astype('str')+'.00'# <<< changing "integer" strings into "decimal" strings
df[a_column][i]=df[a_column][i].astype('float') <<< now it works without "integers" being lost

我不太明白你所说的"失去价值"是什么意思。然后我失去了这些值,如果他们在Excel中显示为(即)100,而不是一个100,00或100.00."也许你的意思是在末尾只加一个小数点。

无论如何,我试着以一种更有效的方式重现你的代码。遍历pandas数据框架的单元格非常慢,每个人都反对这样做。您可以使用一个函数(在这个答案中是lambda函数)并使用.apply()来应用函数:

import pandas as pd
# Create some sample data based on the description
df = pd.DataFrame(data={"unit_selling_price" : ['100,00 ', '92.20 ', '90,00 ', '156']
,"sales_amount" : ['89.45 ', '91.23 ', '45,458 ', '5784']
}
)
columns_to_have_fomat_changed = ["unit_selling_price","sales_amount"]
for column in df[columns_to_have_fomat_changed].columns:
# Replace commas with .
df[column] = df[column].replace(',', '.', regex=True)
# Strip white spaces from left and right side of the strings
df[column] = df[column].str.strip()
# Convert numbers to numeric
df[column] = df[column].apply(lambda x: float(x) if '.' in x else float(str(x)+'.00'))

输出:

unit_selling_price  sales_amount
0   100.0                 89.450
1   92.2                  91.230
2   90.0                  45.458
3   156.0                 5784.000