我需要读取带有逗号的 csv 文件以及字符串和数字,但数字中包含逗号,例如 1,260。此外,csv 文件是用逗号分隔的,因此我无法以正确的方式读取文件。我怎样才能将它们分开?
import pandas as pd df_customer_list=pd.read_csv("customer_list 09.01.2024.csv",sep=',')
该文件包含以下 3 行
angel melo,[email protected],"1,260",Yes,0 michael alem,[email protected],60,Yes,0 charles ekk,[email protected],"2,220",Yes,0
我认为核心问题是您的数据似乎没有标题,因此数据框的显示有点不稳定。
以您的示例数据为例,我似乎只需指定千位分隔符且不指定标题即可正常加载它。
import io import pandas data = """ angel melo,<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="cdaca3aaa8a1a0a8a1a2f8fb8daaa0aca4a1e3aea2a0">[email protected]</a>,"1,260",yes,0 michael alem,<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="55383c363d343930393a3a153238343c397b363a38">[email protected]</a>,60,yes,0 charles ekk,<a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="6a09020b18060f195f5c2a0d070b030644090507">[email protected]</a>,"2,220",yes,0 """ df = pandas.read_csv(io.stringio(data), thousands=",", header=none) print(df)
应该产生:
0 1 2 3 4 0 angel melo <a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="74151a1311181911181b4142341319151d185a171b19">[email protected]</a> 1260 Yes 0 1 michael alem <a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="1a777379727b767f7675755a7d777b737634797577">[email protected]</a> 60 Yes 0 2 charles ekk <a href="https://www.php.cn/link/89fee0513b6668e555959f5dc23238e9" class="__cf_email__" data-cfemail="6e0d060f1c020b1d5b582e09030f0702400d0103">[email protected]</a> 2220 Yes 0
以上是无法用千位和逗号分隔 csv 文件的详细内容。更多信息请关注PHP中文网其他相关文章!