熊猫列表操纵和填充NA

时间:2019-03-06 07:38:41

标签: python pandas function filter

我正在尝试使用此功能,以便从数据帧中提取AdjClose值。

def get_sell_price(data):
    buy_date = get_buy_date(data)
    sell_date = get_sell_date(buy_date)
    l=[]
    for i in range(0,len(buy_date)):
        sell_price = data[(data.Date == sell_date[i])].AdjClose
        l.append(sell_price)
    return l

这将返回数据:

[8180    110.459999
 Name: AdjClose, dtype: float64, 17052    655.679993
 Name: AdjClose, dtype: float64, 17452    968.099976
 Name: AdjClose, dtype: float64, 17453    970.280029
 Name: AdjClose, dtype: float64, 17454    965.719971
 Name: AdjClose, dtype: float64, 17455    955.25
 Name: AdjClose, dtype: float64, 17458    944.159973
 Name: AdjClose, dtype: float64, 17462    950.690002
 Name: AdjClose, dtype: float64, 17470    914.619995
 Name: AdjClose, dtype: float64, 17497    951.640015
 Name: AdjClose, dtype: float64, 17536    977.070007
 Name: AdjClose, dtype: float64, 17537    966.580017
 Name: AdjClose, dtype: float64, 17538    964.0
 Name: AdjClose, dtype: float64, 18180    1335.209961
 Name: AdjClose, dtype: float64, 18181    1313.040039
 Name: AdjClose, dtype: float64, 18182    1285.550049
 Name: AdjClose, dtype: float64, 21116    1514.400024
 Name: AdjClose, dtype: float64, 21424    1300.680054
 Name: AdjClose, dtype: float64, 22006    1178.099976
 Name: AdjClose, dtype: float64, 22016    1196.47998
 Name: AdjClose, dtype: float64, 22017    1197.300049
 Name: AdjClose, dtype: float64, 22018    1210.650024
 Name: AdjClose, dtype: float64, 22537    1209.109985
 Name: AdjClose, dtype: float64, 25106    2914.0
 Name: AdjClose, dtype: float64, 25113    2901.610107
 Name: AdjClose, dtype: float64, 25114    2885.570068
 Name: AdjClose, dtype: float64, 25116    2885.570068
 Name: AdjClose, dtype: float64, 25117    2884.429932
 Name: AdjClose, dtype: float64, 25118    2880.340088
 Name: AdjClose, dtype: float64, 25119    2785.679932
 Name: AdjClose, dtype: float64, 25122    2767.129883
 Name: AdjClose, dtype: float64, 25129    2767.780029
 Name: AdjClose, dtype: float64, 25143    2723.060059
 Name: AdjClose, dtype: float64, 25144    2723.060059
 Name: AdjClose, dtype: float64, 25157    2736.27002
 Name: AdjClose, dtype: float64, 25158    2736.27002
 Name: AdjClose, dtype: float64, 25169    2737.800049
 Name: AdjClose, dtype: float64, 25219    2670.709961
 Name: AdjClose, dtype: float64, 25240    2707.879883
 Name: AdjClose, dtype: float64, Series([], Name: AdjClose, dtype: float64), Series([], Name: AdjClose, dtype: float64)]

我最好更改以下一行

sell_price = data[(data.Date == sell_date[i])].AdjClose

sell_price = data[(data.Date == sell_date[i])].AdjClose.values[0]

所以我只会得到没有附加说明的值列表。

但是,列表中的最后2个项目为空,因此当它尝试提取值时,会导致错误。这是因为数据帧中的sell_date中的2个在2020年,因此它没有数据要返回,从而导致索引错误。

我尝试过滤sell_date <2019-2-28,因为那是我拥有的数据量。但这不起作用,因为整个表需要有41行。

有什么办法可以使用

在该函数中返回值为0的值?
sell_price = data[(data.Date == sell_date[i])].AdjClose.values[0]

感谢您的经验和见解!

1 个答案:

答案 0 :(得分:1)

如果存在第一个值,可以将nextiter一起使用,否则返回默认值(此处为NaN)。

对于带有过滤器的选择列,最好使用DataFrame.loc

sell_price = next(iter(data.loc[(data.Date == sell_date[i]), 'AdjClose']), np.nan)