简体   繁体   English

为什么从带有 Pandas 的 a.CSV 文件中读取的数据在转换为整数后不能使用 matplotlib 绘制?

[英]Why can data read from a .CSV file with Pandas not be plotted using matplotlib after turning it into integers?

My Goal我的目标

Display a bar chart showing the names durations of the first 30 Netflix shows from a.CSV file显示一个条形图,显示来自 .CSV 文件的前 30 个 Netflix 节目的名称持续时间

Relevant Code after Trail & Error Trail & Error 后的相关代码

names = pd.read_csv("netflix_titles.csv", nrows=31, usecols=[2])
durations = pd.read_csv("netflix_titles.csv", nrows=31, usecols=[9])
durations[['duration']] = durations[['duration']].astype(int)

Then I plot it.然后我 plot 吧。

plt.bar(names,durations)
plt.title("Show Durations")
plt.xlabel("Name of Shows")
plt.ylabel("Durations (In Minutes)")
plt.show()

31 rows are read as the first rows are headers.读取 31 行,因为第一行是标题。 durations is turned into integers as the numbers in the column count as string or something else, and wouldn't work with matplotlib.持续时间转换为整数,因为列中的数字计为字符串或其他内容,并且不适用于 matplotlib。

Error Message错误信息

TypeError: unhashable type: 'numpy.ndarray' TypeError:不可散列的类型:'numpy.ndarray'

I don't think Numpy applies with what I'm trying to do, so I'm at a dead end here.我不认为 Numpy 适用于我正在尝试做的事情,所以我在这里陷入了死胡同。

This was able to print out a bar chart for the first 31 values这能够打印出前 31 个值的条形图

dataset = pd.read_csv("netflix_titles.csv")

names = dataset['title'].head(31)
durations = dataset['duration'].head(31)

plt.bar(names,durations)
plt.title("Show Durations")
plt.xlabel("Name of Shows")
plt.ylabel("Durations (In Minutes)")
plt.show

The problem is that your are making two different DataFrames from the csv file and trying to plot them against each other.问题是您正在从 csv 文件中制作两个不同的 DataFrame,并尝试将它们相互对抗 plot。 While this is possible, a much simpler approach is to create a single Dataframe from the selected columns and rows of the csv file and then plot it as demonstrated below:虽然这是可能的,但更简单的方法是从 csv 文件的选定列和行创建单个 Dataframe,然后从 plot 中创建一个,如下所示:

import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv("netflix_titles.csv", nrows=31, usecols=[2,9])
df.columns = ['name', 'duration']
df['duration'] = df['duration'].astype(int)
df.set_index('name', inplace=True)

df.plot(kind = 'bar')
plt.title("Show Durations")
plt.xlabel("Name of Shows")
plt.ylabel("Durations (In Minutes)")
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在不使用 Pandas 的情况下使用 python 和 matplotlib 中的 sqlite 数据库中的数据绘制日期时间吗? - can datetime plotted using data from a sqlite database in python & matplotlib without using pandas? 使用Pandas DataFrame和Matplotlib将CSV文件中的数据处理到绘图中 - Using pandas dataframe and matplotlib to manipulate data from a csv file into a plot 为什么 csv 文件中的整数作为字符串读入 pandas dataframe - Why Integers in csv file read as strings into pandas dataframe 如何使用pandas.read_csv将CSV文件中的数据插入数据框? - How can I insert data from a CSV file into a dataframe using pandas.read_csv? 使用matplotlib绘制的数据外推 - Extrapolation from data plotted using matplotlib 使用 pandas 和 matplotlib 从 csv 文件绘制图表 - Chart from a csv file using pandas and matplotlib 当使用 matplotlib 从 CSV 文件绘制时,X、Y 轴上的 0 值未绘制,我需要绘制它们 - When plotting from a CSV file using matplotlib, 0 values on X, Y axis are not plotting and I need them to be plotted 如何从json文件中读取数据,并使用熊猫将其转换为csv? - How to read data from json file and convert it to csv using pandas? 在Python中使用Pandas从CSV文件读取特定数据 - Using Pandas to read specific data from a CSV file in Python 无法使用 pandas 将数据读取到 csv 文件 - Not able to read data to csv file using pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM