如何求解“ keyError:” [index([[[[umur)''],dtype =' object;)]

发布于 2025-02-02 02:17:28 字数 958 浏览 1 评论 0原文

我有一个

我有一个分配来过滤和绘制数据。我不太了解,所以我只是从讲师的演示文件中复制了代码,但是我自己制作了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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2025-02-09 02:17:28

loc访问行中的单个标签,您可能需要

df5 = df.loc[:, ['Umur']]
# or
df5 = df[['Umur']]

Single label in loc accesses row, you might want

df5 = df.loc[:, ['Umur']]
# or
df5 = df[['Umur']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文