【发布时间】:2016-06-09 13:15:28
【问题描述】:
我正在尝试将计算矩阵的结果附加到我的 df 中。而且我很难看到如何设计我的计算迭代的更大图景。我有以下代码可以说明我正在尝试做的事情。
import pandas as pd
from pandas import DataFrame
import numpy as np
np_all = np.array([[1, 'vws.co', 1],
[1, 'nflx', 3],
[1, 'aapl', 2],
[2, 'vws.co', 1],
[2, 'nflx', 2],
[2, 'aapl', 1],
[3, 'vws.co', 1],
[3, 'nflx', 3],
[3, 'aapl', 1]])
df_all = pd.DataFrame(data=np_all, columns=['Date', 'Ticker', 'Close'])
df_all = df_all.sort(['Ticker','Date'], ascending=[1,1])
df_kpi_list = []
stocklist = ['vws.co','nflx','aapl']
print (df_all)
def screener(df_all,ticker):
# Copy df_all to df for single ticker operations
df = df_all
# filter to only relevant ticker
df = df[df['Ticker'] == ticker]
df = df[df.Ticker == ticker.lower()]
def kpi1_calc(df,ticker):
# do some KPI calculation that are appended to new columns of df
pass
def kpi2_calc(df,ticker):
# do more KPI calculation that are appended to new columns of df
pass
def kpi3_calc(df,ticker):
# example of more KPI calculation that are appended to new columns of df
# Add content to df - RSI
rsi = 3 # stupid example of a constant that is stored in df column
r = rsi
# add a RSI column
r['RSI'] = rsi
df_kpi_list.append(r)
return df
return df
return df
# concatenate all the ticker-iteration dfs from df_kpi_list into one df_all
df_all = pd.concat(df_kpi_list)
return df_all
if __name__ == '__main__':
for ticker in stocklist:
df_data = screener(df_all, ticker)
print (df_data)
我增加了几层数据的复杂性:
- df_kpi_list = [] 是一个空列表,将附加特定于股票代码的 dfs,因此我可以将它们连接到一个新的包含所有内容的 df_all 中。
- df_all 是一个包含我所有股票信息的 df(时间序列数据股票信息的多个股票信息)
- df 相同的信息,但现在只过滤到正在迭代的相关股票代码
- 上述 df(公关代码)将为每个 kpi[no]_calc 函数添加更多信息并添加列 - 并且应该添加到列表中:df_kpi_list = []
处理这些正在计算的信息并最终汇总为一个包罗万象的 df_all 的最聪明的方法是什么?
【问题讨论】:
标签: python pandas append dataframe concat