无法用千位和逗号分隔csv文件

问题描述 投票:0回答:1

我需要读取带有逗号的 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
python regex csv read-csv
1个回答
0
投票
import pandas as pd

# Specify the quote character to ensure proper parsing of fields with commas
df_customer_list = pd.read_csv("Customer_list 09.01.2024.csv", sep=',', quotechar='"')

# Optionally, convert the columns with numbers to integers or floats, if needed
# This step is required if you want to perform numerical operations on these fields
df_customer_list['number_column'] = df_customer_list['number_column'].str.replace(',', '').astype(int)

# Display the DataFrame
print(df_customer_list)

如果您的数字是浮点值,请改用 astype(float)。

© www.soinside.com 2019 - 2024. All rights reserved.