【发布时间】:2018-10-18 17:43:27
【问题描述】:
您好,我想通过一个函数增加一个全局变量“count”,该函数将在长度为 1458 的 pandas 数据帧上调用。
我已经阅读了其他关于 .apply() 没有到位的答案。 因此我听从了他们的建议,但计数变量仍然是 4
count = 0
def cc(x):
global count
count += 1
print(count)
#Expected final value of count is 1458 but instead it is 4
# I think its 4, because 'PoolQC' is a categorical column with 4 possible values
# I want the count variable to be 1458 by the end instead it shows 4
all_data['tempo'] = all_data['PoolQC'].apply(cc)
# prints 4 instead of 1458
print("Count final value is ",count)
【问题讨论】:
-
数据框有 4 列吗?你需要
all_data['PoolQC'].apply(cc, axis=1) -
轴是申请DataFrame,这里我们申请的是Series
标签: python-3.x pandas global-variables apply