【发布时间】:2018-03-05 15:14:37
【问题描述】:
我有字典形式的代码,我想将其转换为 pandas
df = pd.DataFrame(dict(a=[1.5,2.8,9.3],b=[3.2,3.3,3.9],c=[13.1,4.9,15.9],d=[1.1,1.9,2.9]))
这些数据是虚构的,但在实际场景中,我使用的是与此类似的函数。 但是如果 x 大于 4 或小于 2,我该如何修改 if else 呢? 目前我只定义 x 是否大于 4...
def _print(x):
if (x < 4).any():
print('Zone %s does not make setpoint, see box plot below for the distribution of the data in DegF' % x.name)
else:
print('Zone %s meets zone temperature setpoint' % x.name)
print('The average is %s in DegF' % x.mean())
print('---')
df.apply(lambda x: _print(x))
【问题讨论】:
-
if x > 4 or x < 2...!?还是转成if 2 <= x <= 4: ... else: ...……!?或者if x in range(2, 5): ... else: ......!? -
您实际上想要达到什么目的?
df.apply不是为打印字符串而设计的。为此目的使用它是非常低效的。 -
您正在寻找如何使用Logical or operator 或如何编写Elif statements,但是您提出问题的方式很难说出您想要哪个。
-
不太确定你在轴和确切逻辑方面追求什么,但似乎你最好希望得到一个类似的数据框:
df[df.apply(pd.Series.between, args=(2, 4)).any(axis=1)].mean(),然后使用来自生产一些东西...... -
你知道我该如何修改吗?在我的真实数据集上,我还为不符合设定点的数据创建了一个系列箱线图..
标签: python python-3.x pandas