迭代DataFrame行以在引用其他行时创建新列

时间:2016-07-09 17:50:11

标签: python pandas

我有一个我正在使用的大型数据框,其中包含股票的基础数据。下面是数据帧(数据)的头部和尾部的图像。它包含每个证券的数据,每年从2005年到2015年。请注意“日历”'柱。

我的目标是去每一排,享受' revenueusd'数据点并将其除以" revenueusd'前一年的数据点,以获得每个证券的1年收入增长变化。使用股票代码和日历定位第二个数据点。

我一直在尝试使用带有lambda的apply函数,但它不起作用。以下是我一直在尝试的代码:

def conversion(tick, dates,dataframe):
    date1 = datetime.datetime.strptime(dates, "%Y-%m-%d").date()
    date2 = datetime.date(date1.year-1,date1.month,date1.day).strftime("%Y-%m-%d")
    growth = dataframe[(dataframe['ticker']==tick)&(dataframe['calendardate']==dates)]['revenueusd']/dataframe[(dataframe['ticker']==tick)&(dataframe['calendardate']==date2)]['revenueusd']-1
    return growth

data['1yrRevenueGrowth']=data.apply(lambda x: conversion(x['ticker'],x['calendardate'],data),axis=1)

我已经坚持了几天,无情地搜索论坛。任何帮助将不胜感激!

data.head(5)

data.tail(5)

,ticker,ticker.1,calendardate,revenueusd,gp,rnd  
0,A,A,2015-12-31,4038000000,2041000000,330000000  
1,AA,AA,2015-12-31,22534000000,4465000000,238000000  
2,AAL,AAL,2015-12-31,40990000000,23911000000,0  
3,AAP,AAP,2015-12-31,9737018000,4422772000,0  
4,AAPL,AAPL,2015-12-31,234988000000,94308000000,8576000000  
5,ABBV,ABBV,2015-12-31,22859000000,18359000000,4435000000  
509,A,A,2014-12-31,6981000000,3593000000,719000000  
510,AA,AA,2014-12-31,23906000000,4769000000,218000000  
511,AAPL,AAPL,2014-12-31,199800000000,78432000000,6606000000  
512,ABBV,ABBV,2014-12-31,19960000000,15534000000,3649000000 

2 个答案:

答案 0 :(得分:1)

为您的目的,有一个名为Series.pct_change的好功能。你可以这样做:

import pandas as pd
data = pd.read_csv("data.csv", index_col=0)
data.groupby("ticker").apply(lambda x : x.set_index("calendardate").sort_index()["revenueusd"].pct_change())

对于每个股票代码值,构造一个按日历日期排序的系列,然后应用函数pct_change(默认情况下,此函数计算两个连续条目之间的比率)。

ticker  calendardate
A       2014-12-31           NaN
        2015-12-31     -0.421573
AA      2014-12-31           NaN
        2015-12-31     -0.057391
AAL     2015-12-31           NaN
AAP     2015-12-31           NaN
AAPL    2014-12-31           NaN
        2015-12-31      0.176116
ABBV    2014-12-31           NaN
        2015-12-31      0.145240
Name: revenueusd, dtype: float64

还有一件事,您的日期格式很好,因此您可以轻松地将列转换为类型日期时间,如下所示:

data["calendardate"] = pd.to_datetime(data["calendardate"], infer_datetime_format=True)

答案 1 :(得分:0)

从这开始:

 ticker ticker.1 calendardate   revenueusd          gp      rnd  
0      A        A   2015-12-31   4038000000  2041000000  330000000
1     AA       AA   2015-12-31  22534000000  4465000000  238000000
2      A        A   2014-12-31    403800000   204100000  330000000
3     AA       AA   2014-12-31   2253400000   446500000  238000000
4      A        A   2013-12-31    403800000    20410000  330000000
5     AA       AA   2013-12-31    225340000    44650000  238000000
6      A        A   2012-12-31       403800     2041000  330000000
7     AA       AA   2012-12-31     22534000     4465000  238000000


df["pct"] =  df.groupby("ticker")['revenueusd'].pct_change()



 ticker ticker.1 calendardate   revenueusd          gp      rnd      pct
0      A        A   2015-12-31   4038000000  2041000000  330000000    NaN
1     AA       AA   2015-12-31  22534000000  4465000000  238000000    NaN
2      A        A   2014-12-31    403800000   204100000  330000000 -0.900
3     AA       AA   2014-12-31   2253400000   446500000  238000000 -0.900
4      A        A   2013-12-31    403800000    20410000  330000000  0.000
5     AA       AA   2013-12-31    225340000    44650000  238000000 -0.900
6      A        A   2012-12-31       403800     2041000  330000000 -0.999
7     AA       AA   2012-12-31     22534000     4465000  238000000 -0.900

您可能需要在应用groupby之前对DataFrame进行排序