如何求解“ keyError:” [index([[[[umur)''],dtype =' object;)]
我有一个
我有一个分配来过滤和绘制数据。我不太了解,所以我只是从讲师的演示文件中复制了代码,但是我自己制作了CSV文件。当我试图运行剧情时,它不起作用。这就是它所说的。
我想制作一个条形图,以显示同龄人的人数。如果可能的话,如何制作饼图并显示百分比?
顺便说一句,“ umur”是指年龄
import pandas as pd
from pathlib import Path
df = pd.read_csv('inicsvanakbisdig.txt')
filepath = Path('tugaspertemuan12afk.csv')
df.to_csv(filepath)
#Column Selection
df1 = df['Nama']
print(df1)
#Select row where 'Umur' is equal to 20
df2 = df.loc[df['Umur'] == 20]
print(df2)
#Drop Kolom 'Umur'
df3 = df.drop(['Umur'], axis=1)
print(df3)
#Computes a summary of statistics
df4 = df.describe()
print(df4)
#Plot
import matplotlib.pyplot as plt
df5 = df.loc[['Umur']]
p = df5['Umur'].sort_index()
p.plot(kind = 'bar',title = 'Umur anak bisdig', xlabel = "Umur", ylabel = "Counter")
plt.show()
I have a CSV file for the code I wrote.
I have an assignment to filter and plot data. I didn't really understand, so I just copied the code from my lecturer's presentation file, but I made the CSV file myself. When I tried to run the plot, it didn't work. This is what it said.
I want to make a bar chart to show the number of people with the same age. If it's possible, how do I make a pie chart instead, and show the percentage?
btw, "Umur" means Age
import pandas as pd
from pathlib import Path
df = pd.read_csv('inicsvanakbisdig.txt')
filepath = Path('tugaspertemuan12afk.csv')
df.to_csv(filepath)
#Column Selection
df1 = df['Nama']
print(df1)
#Select row where 'Umur' is equal to 20
df2 = df.loc[df['Umur'] == 20]
print(df2)
#Drop Kolom 'Umur'
df3 = df.drop(['Umur'], axis=1)
print(df3)
#Computes a summary of statistics
df4 = df.describe()
print(df4)
#Plot
import matplotlib.pyplot as plt
df5 = df.loc[['Umur']]
p = df5['Umur'].sort_index()
p.plot(kind = 'bar',title = 'Umur anak bisdig', xlabel = "Umur", ylabel = "Counter")
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
loc
访问行中的单个标签,您可能需要Single label in
loc
accesses row, you might want