如何使用pandas计算与初始值相比的百分比变化?

时间:2016-01-29 17:28:46

标签: python pandas dataframe percentage

我有DataFrame并且需要计算公司与年初相比的变化百分比。有没有办法使用pct_change()或其他方法来执行此任务?谢谢!

df看起来像

security    date        price
IBM         1/1/2016    100
IBM         1/2/2016    102
IBM         1/3/2016    108
AAPL        1/1/2016    1000
AAPL        1/2/2016    980
AAPL        1/3/2016    1050
AAPL        1/4/2016    1070

我想要的结果

security    date        price   change
IBM         1/1/2016    100     NA
IBM         1/2/2016    102     2%
IBM         1/3/2016    108     8%
AAPL        1/1/2016    1000    NA
AAPL        1/2/2016    980     -2%
AAPL        1/3/2016    1050    5%
AAPL        1/4/2016    1070    7%

3 个答案:

答案 0 :(得分:4)

听起来您正在寻找pct_change()版本的df.groupby('security')['price'].apply(lambda x: x.div(x.iloc[0]).subtract(1).mul(100)) 。这并不是现成的AFAIK,但你可以自己动手:

if($stmt = $this->db->prepare("SELECT table_users.id AS user_id, table_users.email AS user_email, table_users.GUID as user_guid, "
        . "table_roles.slug AS role_slug, table_user_settings.username, table_users.id_roles, "
        . "table_users.first_name, table_users.last_name, table_users.mobile_number, table_users.phone_number, "
        . "table_users.address, table_users.city, table_users.state, table_users.zip_code, table_users.notes "
        . "FROM table_users "
        . "WHERE table_users.data = 0 "
        . "INNER JOIN table_roles ON table_roles.id = table_users.id_roles "
        . "INNER JOIN table_user_settings ON table_user_settings.GUID = table_user.GUID "
        . "WHERE table_user_settings.username = ? "
        . "WHERE table_user_settings.password = ? "))
    {
        $stmt->bind_param("ss",$username, $password);
        $stmt->bind_result($id, $email, $GUID, $slug, $id_roles, $address, $city, $state, $zip_code, $notes);
        $result = $stmt->execute();
        $stmt->fetch();
    }

    var_dump($this->db->error);

    $stmt->close();
    return $result;

答案 1 :(得分:1)

这是有效的,假设您已在每个可能的分组中按日期排序。

def pct_change(df):
    df['pct'] = 100 * (1 - df.iloc[0].price / df.price)
    return df

df.groupby('security').apply(pct_change)

答案 2 :(得分:0)

我遇到了同样的问题,但是用他的方式解决了:

(唯一的区别是,列将是您的公司而不是行。)

对于数据框的每一列,我都做了

df[column] = df[column].pct_change().cumsum()

pct_change()计算从现在到最后一个值之间的变化,然后cumcum()将它们相加。