如何在熊猫中连续打印大于80的值?

时间:2019-01-07 18:10:50

标签: python pandas

我希望我的代码为list3中大于80的行返回列表1中值的百分比变化。

import pandas as pd

import numpy as np

df= pd.DataFrame({"list1":[2,5,4,8,4,7,8],"list2": 
[5,8,9,8,7,5,5],"list3": 
[50,65,80,82,89,90,76]})


def PCT(x):

      return df[x].pct_change()*100

for row in df["list3"]:

if row >= 80:
    df["%change_list1"]=df.apply(lambda row: PCT(["list1"]),axis=1)
else:
    df["%change_list1"]=np.NaN

df

1 个答案:

答案 0 :(得分:0)

修复代码

def PCT(x):
    return df[x].pct_change() * 100
for x,row in zip(df.index,df["list3"]):
    if row >= 80:
        df.loc[x, "%change_list1"] = PCT('list1').loc[x]
    else:
        df.loc[x,"%change_list1"] = np.NaN
df
Out[366]: 
   list1  list2  list3  %change_list1
0      2      5     50            NaN
1      5      8     65            NaN
2      4      9     80          -20.0
3      8      8     82          100.0
4      4      7     89          -50.0
5      7      5     90           75.0
6      8      5     76            NaN