【发布时间】:2018-08-01 04:57:11
【问题描述】:
我有很多字典格式的数据,我正在尝试使用 pandas 打印基于 IF ELSE 语句的字符串。对于我的示例,我将在 dict 中编造一些数据并隐蔽到 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 声明:
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系列,但是pandas.Series.plot.box()可以用吗?
这是我在 df.apply(lamba x:) 的函数中使用的 IF ELSE 语句,我一直试图让箱形图在熊猫系列中工作......非常感谢任何建议!
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'
【问题讨论】:
-
我无法重现您的错误(according to this 在您添加
import matplotlib.pyplot as plt时是否已修复)?如果是,你能更新你的问题吗?
标签: python python-3.x pandas matplotlib data-science