python:迭代ya dataframe列作为打印图表的函数中的值

发布于 2025-02-09 16:36:26 字数 775 浏览 0 评论 0原文

我正在尝试通过数据框中的数字字段迭代,并为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 技术交流群。

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

发布评论

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

评论(1

提笔书几行 2025-02-16 16:36:26

您的程序在尝试将“名称”列中的数据与可变定义线路中的整数值进行比较之前,在将其移动到其他两个列之前存在问题。

data = df[(df.columns > 80 )].groupby(df.Name, as_index = True).agg({columns: "sum"})

该列中的值是使函数失败的字符串。通过一些反复试验,我将您的程序修改为仅对第二列和第三列进行比较(“ Test1”和“ Test2”)。以下是修订的代码。

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['Test1'] > 20) | (df['Test2'] > 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)

plt.show()

运行该程序产生了两个条形图。

您可能需要尝试比较值,但我认为这应该为您提供以下信息的信息。

希望有所帮助。

问候。

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.

data = df[(df.columns > 80 )].groupby(df.Name, as_index = True).agg({columns: "sum"})

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.

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['Test1'] > 20) | (df['Test2'] > 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)

plt.show()

Running that program produced the two bar charts.

Sample 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.

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