Python Pandas Series if else箱形图 [英] Python Pandas Series if else box plot
问题描述
我有很多词典格式的数据,并且我尝试使用pandas根据IF ELSE语句打印字符串.在我的示例中,病假在dict和Pandas的秘密数据中组成了一些数据:
I have alot of data in a dictionary format and I am attempting to use pandas print a string based on an IF ELSE statement. For my example ill make up some data in dict and covert to Pandas:
df = pd.DataFrame(dict(a=[1.5,2.8,9.3],b=[7.2,3.3,4.9],c=[13.1,4.9,15.9],d=[1.1,1.9,2.9]))
df
这将返回:
a b c d
0 1.5 7.2 13.1 1.1
1 2.8 3.3 4.9 1.9
2 9.3 4.9 15.9 2.9
我的IF ELSE声明:
My IF ELSE statement:
for col in df.columns:
if (df[col] < 4).any():
print('Zone %s does not make setpoint' % col)
else:
print('Zone %s is Normal' % col)
返回:
Zone a does not make setpoint
Zone b does not make setpoint
Zone c is Normal
Zone d does not make setpoint
但是现在我想添加一个额外的东西来创建一个箱形图,在该图上我不设定设定值,并且还要对数据框进行设定的平均值.我知道这是熊猫系列,但是可以使用pandas.Series.plot.box()
吗?
But now I want to add in an extra to create a box plot where I am not making setpoint and also average the data frame where it is making setpoint. I know this is pandas series, but can pandas.Series.plot.box()
be used?
这是我在df.apply(lamba x:)
函数中使用的IF ELSE语句,我被困于试图让盒形图在熊猫系列中工作...任何建议,我们将不胜感激!
This is my IF ELSE statement that I am using in a function with df.apply(lamba x:)
and I am stuck trying to get the box box plot to work in pandas series... Any advice is greatly appreciated!
import matplotlib.pyplot as plt
def _print(x):
if (x < 4).any():
print('Zone %s does not make setpoint' % x.name)
df.boxplot()
plt.show()
else:
print('Zone %s is Normal' % x.name)
print('The average is %s' % x.mean())
致电df.apply(lambda x: _print(x))
module 'matplotlib' has no attribute 'show'
推荐答案
您可以像df['a'].plot.box()
一样调用pandas.Series.plot.box()
来获取列a
的箱线图.
Sure you can call pandas.Series.plot.box()
like df['a'].plot.box()
to get the boxplot of your column a
.
为了满足您的问题,我会这样做:
To fit with your question I would have done this:
def _print(x):
if (x < 4).any():
print('Zone %s does not make setpoint' % x.name)
df[x.name].plot.box() #call x.name to retrieve the column name
plt.show()
print(df[x.name].describe())
else:
print('Zone %s is Normal' % x.name)
print('The average is %s' % x.mean())
print('---')
df.apply(lambda x: _print(x))
下面在zone B
和zone C
的输出摘要中进行了说明.
Illustrated below extract of the output for zone B
and zone C
.
请注意,您可以添加.describe()
以获得箱线图和其他统计信息描述(请参见
Note that you can add .describe()
to get the boxplot and other stats description (see documentation).
不过,根据此处提出的解决方案,我将以不同的方式处理该问题.
Nevertheless I would have approach the problem differently, according to the solution proposed here.
另一种解决方案
您可以过滤数据框以拆分为make setpoint与否:
You can filter your dataframe to split into make setpoint or not:
s = df.apply(lambda x: not (x < 4).any())
然后在未设置设定值的框上绘制框.
如果变化不太大且区域不多,则在图中绘制所有图形:
Then plot the boxes on the one that doesn't make the set point.
Plot all in a figure if the variation is not too large, and if there is not so many zones:
df[s[~s].index].boxplot()
plt.show()
或将它们分开:
for col in s[~s].index:
df[col].plot.box()
plt.show()
在两种情况下,都将统计信息保存在dataframe
中:
In both case get the statistics in a dataframe
:
statdf = df[s[~s].index].describe()
print(statdf)
a b d
count 3.000000 3.000000 3.000000
mean 4.533333 5.133333 1.966667
std 4.178915 1.960442 0.901850
min 1.500000 3.300000 1.100000
25% 2.150000 4.100000 1.500000
50% 2.800000 4.900000 1.900000
75% 6.050000 6.050000 2.400000
max 9.300000 7.200000 2.900000
通过这种方式,您可以通过statdf.loc['mean']
获取统计信息(例如"mean
").
This way you can get the stat (say 'mean
' for instance) with statdf.loc['mean']
.
如果要打印确实达到设定点的平均值:
If you want to print the mean of the one that does make the set point:
print(df[s[s].index].mean())
c 11.3
Name: mean, dtype: float64
这篇关于Python Pandas Series if else箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!