我需要讀取帶有逗號的 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中文網其他相關文章!