gpt4 book ai didi

python - key 错误 : item not in index while trying to build boxplot with pandas

转载 作者:行者123 更新时间:2023-11-30 22:17:46 26 4
gpt4 key购买 nike

我正在尝试从一个简单的数据框中构建一个箱线图。我采用多列的一列,只采用我需要的两列:

mydata = data[['addr_state','loan_amnt']].dropna().copy()
mydata['loan_amnt'] = mydata['loan_amnt'].astype(float)
mydata[:5]

addr_state loan_amnt
0 AZ 5,000.00
1 GA 2,500.00
2 IL 2,400.00
3 CA 10,000.00
4 OR 3,000.00

mydata.columns

Index(['addr_state', 'loan_amnt'], dtype='object')

基本上,它是一堆状态金额记录,我正在尝试为其构建蜡烛:

mydata.boxplot(column='addr_state')

KeyError: "['addr_state'] not in index"

我尝试了set_index()和rebuild_index(),但似乎都没有修复它。

最佳答案

根据 docscolumn 参数应该是运行箱线图的数值(输入到 groupby 而不是其参数),by 参数是分组变量:

mydata.boxplot(column='loan_amnt', by='addr_state')

用随机数据进行演示:

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

np.random.seed(1212)
mydata = pd.DataFrame({'loan_amnt': np.random.randn(50)*100,
'tool': ["".join(np.random.choice(['pandas', 'r', 'julia',
'sas', 'stata', 'spss'],1)) for _ in range(50)]})

mydata.boxplot(column='loan_amnt', by='tool')

Boxplot Output

关于python - key 错误 : item not in index while trying to build boxplot with pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49544123/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com