Python 如何使用元组制作散点图
我下面的代码有问题。尝试从元组创建散点图:Python 如何使用元组制作散点图,python,Python,我下面的代码有问题。尝试从元组创建散点图: #Convert CSV file to dictionary to store data reader = csv.reader(open('winemag-data-130k-v2.csv')) number = {} for row in reader: key = row[0] number[key] = row[1:] #Convert dictionary data to tuples t = list(number.it
#Convert CSV file to dictionary to store data
reader = csv.reader(open('winemag-data-130k-v2.csv'))
number = {}
for row in reader:
key = row[0]
number[key] = row[1:]
#Convert dictionary data to tuples
t = list(number.items())
#Make scatter plot from tuples
# create a figure and axis
fig, ax = plt.subplots()
# scatter the points against the price
ax.scatter(t['[points]'], t['price'])
# set a title and labels
ax.set_title('Wines')
ax.set_xlabel('points')
ax.set_ylabel('price')
我在ax.scatter行中不断得到“TypeError:列表索引必须是整数或片,而不是str”
当您打印它时,它看起来是这样的:
...
('6257',
[“我们”,
“烤橡木和黑胡椒首先影响味觉和嗅觉,然后是”
口感清香,单宁干爽,黑樱桃和香草味,温和平衡
“赤霞珠由12%的小维多(Petit Verdot)制成,增加了周长和口感”
“强度不会变得极端。”,
'',
'90',
'60.0',
“加州”,
“维德山”,
“纳帕”,
“维吉尼·布恩”,
“@vboone”,
“Renteria 2012卡本妮苏维翁(维德山)”,
“赤霞珠”,
“Renteria”]),
我正在尝试访问“90”和“60.0”。有什么想法吗?您的csv数据是什么样子的?还有,为什么不使用熊猫呢?同样在你的ax.scatter中,检查引号在哪里。它应该是
t['points']
而不是t'[points]'
。更新了帖子以显示我不理解的t的样子。这是一排吗?标题是什么?请尝试df=pd.read_csv('winemag-data-130k-v2.csv')
,然后分享前几行的外观<代码>df.head()。(您需要将熊猫导入为pd
)。