【问题标题】:A loop with figures and plots带有图形和绘图的循环
【发布时间】:2018-09-06 14:33:42
【问题描述】:

我正在尝试做一个循环,其中每个i 都有一个带有两个图的图形。图之间的差异由二元列给出。例如:

train = pd.DataFrame({'vehicle': ['car', 'truck', 'bus', 'car', 'bus' ], 
                   'sex': ['male','male','male','female','female'],
                   'income': ['60000', '50000', '65000', '70000', '60000'],
                   'age': [31,25,67,90,18],
                   'crash' : [1,0,0,0,1]
})

每个图必须有两个图,它显示了所有变量和crash 列之间的关系,每个列都有两个案例(crash=1 and crash=0)。这是我的代码,但它仍然无法正常工作:

for i in train.columns:
    i_1 = train[i][train["crash"] == 1]
    i_0 = train[i][train["crash"] == 0]

    fig = plt.figure(figsize=(5, 10)) 
    fig.add_subplot(1, 2, 2)
    ax1 = fig.add_subplot(1,1,1)
    ax2 = fig.add_subplot(1,2,2)
    ax1.plot(train[i], i_1)
    ax2.plot(train[i], i_0)
    plt.show()

所以,图 1 有两个图表。图 1 是具有 crash=1 的车辆的 count,图 2 是具有 crash=0 的车辆的 count。图2,图1是与crash=1发生性关系的count,图2是与crash=0发生性关系的count。依此类推……总共(在本例中)为 4 个数字。

有什么想法吗?

【问题讨论】:

  • 有什么问题? “它不起作用”不是问题陈述。

标签: python pandas loops matplotlib plot


【解决方案1】:

您需要使用聚合来获取计数指标。

以下示例应该可以帮助您入门:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

train = pd.DataFrame({'vehicle': ['car', 'truck', 'bus', 'car', 'bus' ], 
                   'sex': ['male','male','male','female','female'],
                   'income': ['60000', '50000', '65000', '70000', '60000'],
                   'age': [31,25,67,90,18],
                   'crash' : [1,0,0,0,1]
})

for i in train.columns:
    if i == 'crash':
        continue

    table = pd.pivot_table(train[[i, 'crash']], index=[i], aggfunc=np.sum)
    table.plot.bar()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2020-02-17
    • 2022-12-10
    • 2016-06-21
    • 2020-11-25
    • 1970-01-01
    相关资源
    最近更新 更多