使用pandas.read_csv时如何忽略引号内的一些逗号?

时间:2019-06-14 21:23:01

标签: python pandas csv

ID,name,category,main_category,currency,deadline,goal,launched,pledged,state,backers,country,usd pledged
1009317190,French Cuisine, A Traditional Experience,Cookbooks,Food,USD,2014-09-08 00:46:23,13730,2014-08-09 03:16:02,3984,failed,46,US,3984

我使用pandas.read_csv()将上面的csv文件加载到数据帧。但是,我的输出是这样的:

dataframe

问题:如何忽略French CuisineA Traditional Experience之间的逗号,并将它们读入同一列?

2 个答案:

答案 0 :(得分:1)

您可以按照以下步骤实现所需的目标:

第一步:

df['name'] = df['name']+df['category']

enter image description here

第二步:

data1 = df.iloc[:, :2] # dataframe with columns 'ID' and 'name'
data2 = df.iloc[:, 2:].T.shift(-1,axis=0).T # Shifting multi-column data to the left    
data = pd.concat([data1, data2], axis=1)    # concat dataframes data1 and data2 along columns 

enter image description here

Step3:

data = data.drop('Unnamed:13', 1) # drop column named 'Unnamed:13'

enter image description here

答案 1 :(得分:0)

只需打开CSV文件作为文本文件,然后将French Cuisine, A Traditional Experience替换为French Cuisine A Traditional Experience

csv_file = open("example.csv", 'r').read()
csv_file = csv_file.replace("French Cuisine, A Traditional Experience", "French Cuisine A Traditional Experience")

open("example.csv", 'w').write(csv_file)