gpt4 book ai didi

python - 计算 pandas 列中的正值

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:36 24 4
gpt4 key购买 nike

我有一个包含多列数字的表格。我正在尝试获取此表的统计信息,但我一直在尝试计算单列中各行之间的正增长量。

我有什么

A  |  B  |  C
--------------
1 | 5 | 2
5 | 2 | 7
2 | 2 | 10
18 | 0 | 14

我正在尝试为我的问题编写函数

def countIncreace(data,value):  #not complete but what I have so far
print( data[value].pct_change().fillna(0).count( > 0) )

pct_change() 返回该索引处的数字与其之前行中的数字相比的百分比表,fillna(0) 替换 pct_change() 用 0

创建的图表位置 0 中的 NaN

我想要的输出:

[IN]>countIncreace(df, 'A'):
[OUT]> 2
[IN]>countIncreace(df, 'B'):
[OUT]> 0
[IN]>countIncreace(df, 'C'):
[OUT]> 3

我在获取正值计数的函数中缺少什么

最佳答案

非常接近!除了您尝试计算大于 0 的百分比变化数的方式外,一切都是正确的。这是一种方法

def countIncrease(df,column):
print sum(df[column].pct_change().fillna(0) > 0)

关于python - 计算 pandas 列中的正值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51995287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com