pandas 在 Python 中从数据集绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42777946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting from dataset in Python
提问by Sachin Kamble
This must be very simple but i am not able to figure out how to do it.I am trying to plot the data present in my dataset.
这一定非常简单,但我无法弄清楚如何去做。我正在尝试绘制数据集中存在的数据。
Below is my code ,
下面是我的代码,
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('TipsReceivedPerMeal.csv')
plt.scatter(dataset[0],dataset[1])
plt.show()
The data in my CSV file is some random data, which specifies what tip a waiter receive at one particular day.
我的 CSV 文件中的数据是一些随机数据,它指定了服务员在某一天收到的小费。
Data in CSV
CSV 格式的数据
MealNumber TipReceived
1 17
2 10
3 5
4 7
5 14
6 25
Thanks in advance for the help.
在此先感谢您的帮助。
回答by Miriam Farber
Another option is to replace
plt.scatter(dataset[0],dataset[1])
with
另一种选择是替换
plt.scatter(dataset[0],dataset[1])
为
plt.scatter(dataset[[0]],dataset[[1]])
回答by Ankit Malik
Just replace:
plt.scatter(dataset[0],dataset[1])
只需更换:
plt.scatter(dataset[0],dataset[1])
With:
plt.scatter(dataset['MealNumber'],dataset['TipReceived'])
和:
plt.scatter(dataset['MealNumber'],dataset['TipReceived'])
In Pandas columns can either be referenced by name or by column number with iloc.
在 Pandas 中,列可以通过名称或通过 iloc 的列号进行引用。
回答by ImportanceOfBeingErnest
There are several options, some already mentionned in previous answers,
有几个选项,有些已经在之前的答案中提到了,
plt.scatter(dataset['MealNumber'],dataset['TipReceived'])
(as mentioned by @Ankit Malik)plt.scatter(dataset.iloc[:,0],dataset.iloc[:,1])
plt.scatter(dataset[[0]],dataset[[1]])
(as mentioned by @Miriam)
plt.scatter(dataset['MealNumber'],dataset['TipReceived'])
(如@Ankit Malik 所述)plt.scatter(dataset.iloc[:,0],dataset.iloc[:,1])
plt.scatter(dataset[[0]],dataset[[1]])
(如@Miriam 所述)
In order for those to work with the data from the question, one should use the delim_whitespace=True
paramter, as otherwise the read-in would not work:
为了让那些使用问题中的数据,应该使用delim_whitespace=True
参数,否则读入将不起作用:
dataset = pd.read_csv('TipsReceivedPerMeal.csv', delim_whitespace=True)