【问题标题】:python pandas iterrow sum multiplication division subtractionpython pandas iterrow 和乘除减法
【发布时间】:2017-10-16 18:14:08
【问题描述】:

尝试对时间序列数据和迭代执行操作。我已经能够构建一个在日期上添加值的函数,但不使用 pandas。另外,不知道如何使用乘法,减法,除法等。

from operator import *
import pandas as pd
import requests

prices_url = r'https://gist.githubusercontent.com/ryan413/596811e3cd15ad891f7cb3cf8ab7c78b/raw/af359e13b26af7699a2afedc9b148dbb19df25b3/prices.csv'

r = requests.get(prices_url)

with open("prices.csv", 'w', encoding='utf-8') as f:
    f.write(r.text)

df = pd.read_csv('prices.csv',index_col=0, header=0)

# df.columns = Index(['EUR', 'CAD', 'NOK', 'SPY', 'EUR.1'], dtype='object')

#                  EUR       CAD        NOK        SPY      EUR.1
# 2/7/2017   31.234616  5.117412  72.662151  93.685559  73.176038
# 2/8/2017   31.428942  5.471865  72.547450  93.860313  73.889394
# 2/9/2017   30.871875  4.984493  73.030060  94.652531  72.944198
# 2/10/2017  30.716414  4.918033  73.614387  95.182618  75.887682
# 2/13/2017  30.133437  5.305716  74.164088  95.916584  76.568937


def add_rows(df_prices):
    row_sum = {}
    for idx, col in df_prices.iterrows():
        row_sum[idx] = sum(col)
    return row_sum

sum_of_rows = add_rows(df)

for k,v in sum_of_rows.items():
    print(k," - ", v)

# 2/7/2017  -  275.875777014
# 2/8/2017  -  277.197963968
# 2/9/2017  -  276.483156429
# 2/10/2017  -  280.319134197
# 2/13/2017  -  282.088761272
# 2/14/2017  -  280.555245767
# 2/15/2017  -  281.487035784
# 2/16/2017  -  281.282552346
# 2/17/2017  -  280.508299074

# values in each row are added together

Q1:有没有更简单的方法可以用 pandas 做到这一点?

def multiplication(df_prices):
    row_multiply = {}
    for idx, col in df_prices.iterrows():
        row_multiply[idx] = mul(col,col)
    return row_multiply

mult_of_rows = multiplication(df)

for k, v in mult_of_rows.items():
    print(k, " - ", mul(v, v))

# 
# 3/3/2017  -  EUR       926.858939
# CAD         7.304602
# NOK      5603.190074
# SPY      9842.184083
# EUR.1    5915.492570
# Name: 3/3/2017, dtype: float64
# trying to multiply each of the the values with each other

# EUR * CAD * NOK * SPY = value I'm trying to get for each of the rows

Q2:我如何用乘法做同样的事情?分配?减法?

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    给你:

    sum cols:
    
    df.sum(axis=1)
    
    multiply:
    
    import numpy as np
    df.apply(lambda row: np.prod(row), axis=1)
    

    如果您想对各列求和,您可以简单地将 axis 从 1 更改为 0(恕我直言)。

    也可以与常数相乘/除:

    df['Eurocents'] = df.EUR * 100
    

    或者对于整个数据框L

    df_in_cents = df * 100
    

    我希望这就是你要找的。​​p>

    【讨论】:

    • 酷。谢谢
    猜你喜欢
    • 2019-01-27
    • 2019-10-31
    • 1970-01-01
    • 2023-02-04
    • 2020-02-13
    • 1970-01-01
    • 2019-07-19
    • 2023-04-07
    • 2018-10-10
    相关资源
    最近更新 更多