python:迭代ya dataframe列作为打印图表的函数中的值
我正在尝试通过数据框中的数字字段迭代,并为Test1创建两个单独的条形图,而另一个则用于按名称分组的Test2分数。我有一个循环,我会遇到类型错误。我有以下数据的少量样本,但是对于大于25个字段的数据框架,循环将运行。以下是我的代码和错误:
import pandas as pd
import matplotlib.pyplot as plt
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John', 'Tom', 'Joseph', 'Krish', 'John'],
'Test1': [20, 21, 19, 18, 30, 33, 12, 10],
'Test2': [78, 89, 77, 91, 95, 90, 87, 70]}
df = pd.DataFrame(data)
for columns in df.columns[1:]:
data = df[(df.columns > 80 )].groupby(
df.Name, as_index = True).agg(
{columns: "sum"})
fig, (ax) = plt.subplots( figsize = (24,7))
data.plot(kind = 'bar', stacked = False,
ax = ax)
TypeError: '>' not supported between instances of 'str' and 'int'
I'm trying to iterate through numeric fields in a data frame and create two separate bar charts one for Test1 and another for Test2 scores grouped by Name. I have a for loop that I get a type error on. I have a small sample of the data below but this for loop would run for data frame larger than 25 fields. Below is my code and error:
import pandas as pd
import matplotlib.pyplot as plt
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John', 'Tom', 'Joseph', 'Krish', 'John'],
'Test1': [20, 21, 19, 18, 30, 33, 12, 10],
'Test2': [78, 89, 77, 91, 95, 90, 87, 70]}
df = pd.DataFrame(data)
for columns in df.columns[1:]:
data = df[(df.columns > 80 )].groupby(
df.Name, as_index = True).agg(
{columns: "sum"})
fig, (ax) = plt.subplots( figsize = (24,7))
data.plot(kind = 'bar', stacked = False,
ax = ax)
TypeError: '>' not supported between instances of 'str' and 'int'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的程序在尝试将“名称”列中的数据与可变定义线路中的整数值进行比较之前,在将其移动到其他两个列之前存在问题。
该列中的值是使函数失败的字符串。通过一些反复试验,我将您的程序修改为仅对第二列和第三列进行比较(“ Test1”和“ Test2”)。以下是修订的代码。
运行该程序产生了两个条形图。
您可能需要尝试比较值,但我认为这应该为您提供以下信息的信息。
希望有所帮助。
问候。
Your program was having an issue with attempting to compare the data in the "Name" column with the integer value that you had in the variable definition line before it would move along to the other two columns.
The values in that column are strings which makes the function fail. Through some trial and error, I revised your program to just perform comparisons on columns two and three ("Test1" and "Test2"). Following is the revised code.
Running that program produced the two bar charts.
You might want to experiment with the comparison values, but I think this should provide you with the information to move forward on your program.
Hope that helped.
Regards.