尝试对时间序列数据和iterrows执行操作。我已经能够构建一个在日期添加值但不使用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:我如何通过乘法做同样的事情?师?减法?
答案 0 :(得分: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
或整个dataframeL
df_in_cents = df * 100
我希望这就是你要找的东西。