我将python列表转换为Pandas数据帧,然后将数据帧写入csv。清单如下:
name = ['james beard', 'james beard']
ids = [304589, 304589]
year = [1999, 1999]
co_authors = [['athman bouguettaya', 'boualem benatallah', 'lily hendra', 'kevin smith', 'mourad quzzani'], ['athman bouguettaya', 'boualem benatallah', 'lily hendra', 'kevin smith', 'mourad quzzani']]
title = ['world wide databaseintegrating the web corba and databases', 'world wide databaseintegrating the web corba and databases']
venue = ['international conference on management of data', 'international conference on management of data']
data = {
'Name': name,
'ID': ids,
'Year': year,
'Co-author': co_authors,
'Title:': title,
'Venue:': venue,
}
df = pd.DataFrame(data, columns=['Name','ID','Year','Co-author','Title', 'Venue'])
df
df.to_csv('test.csv')
我的问题是
(a)“标题”和“地点”列显示为“NaN”,而不是它们的值(见下文)。我该怎么解决这个问题?在
^{pr2}$(b)在CSV(见下文)中,如何在标题中添加“Index”并删除“Co-author”中的括号?在
,Name,ID,Year,Co-author,Title,Venue
0,james beard,304589,1999,"['athman bouguettaya', 'boualem benatallah', 'lily hendra', 'kevin smith', 'mourad quzzani']",,
1,james beard,304589,1999,"['athman bouguettaya', 'boualem benatallah', 'lily hendra', 'kevin smith', 'mourad quzzani']",,
啊,好吧,我解决(b)在加载到数据之前。。在
至于第一个问题:在},
data
中,名称'Title:'
,'Venue:'
所以
DataFrame
在data
中找不到{'Venue'
。在您必须删除
:
或者您可以跳过
columns=[...]
,它将使用带有:
-'Title:'
,'Venue:'
的名称至于第二个问题:我在创建
DataFrame
之后(或期间)用pandas
搜索解决方案。但我没找到。在
但是如果您假设您可以在创建
^{pr2}$DataFrame
之前修改数据,那么您可以编写更短的版本相关问题 更多 >
编程相关推荐