我有2个字段" market_cap"和" debt_ratio"我在哪里使用pandas cut功能,以便根据market_cap创建5个子组。
我有兴趣创建5个盒子图(每个子组),但要显示的数据是debt_ratio。
cleaned_data = ( cleaned_data.groupby( pd.cut( cleaned_data['market_cap_(in_us_$)'], 5 ) )['market_debt_to_capital_ratio'] )
# Create a figure instance
fig = plt.figure( 1, figsize = ( 9, 5 ) )
# Create an axes instance
ax = fig.add_subplot( 111 )
# Create the boxplot
bp = ax.boxplot( cleaned_data )
# Save the figure
fig.savefig( 'fig1.png', bbox_inches = 'tight' )
但是,我收到以下错误
File "C:...\box_plots.py", line 29, in <module>
bp = ax.boxplot( cleaned_data[1] )
File "C:\Python27\lib\site-packages\pandas\core\groupby.py", line 489, in __getitem__
raise NotImplementedError
NotImplementedError
答案 0 :(得分:1)
您可以通过将各个组添加到列表中来生成箱图。
test = []
for name, group in cleaned_data:
test.append(group)
boxplot(test)