-1
我想追加一個計算矩陣的結果給我的df。我有一個問題,就是如何設計我的迭代計算的大局。我有以下代碼應該舉例說明我正在嘗試做什麼。如何將迭代(從每個iter。)結果追加到包含所有迭代結果的最終Dataframe?
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將被附加到,所以可全部Concat的這些到一個新的最終包含df_all。
- df_all是用我所有的stockinfo一個DF(時間序列數據stockinfo多行情資訊)
- DF相同的信息,但現在過濾,只相關股票被迭代
- 以上DF(PR股票)將每個KPI [無] _calc功能添加更多的信息與所添加的列 - 並且應該被添加到列表:df_kpi_list = []
正在計算什麼是處理這些信息的最聰明的方式,最後總結成一個包羅萬象的df_all?
感謝大衛, 但是這些計算需要在大熊貓(數據框)中就地完成,因爲我正在計算超過3000種股票的10年日期時間系列(日間)股票〜超過10兆歐。行需要計算。所以數據必須保存到df。 I.e.你的子彈1。 – Excaliburst