私はPythonとパンダを学び、VBAから来てみようとしています。私はまだ各セルをループする習慣に巻き込まれていますが、一度に全行を操作する方法を探しています。より効率的なパンダコード
以下は自分のコードの一部です。私は列に約3000株、dfと呼ばれるデータフレームに保存された行に約40程度のデータポイントを持っています。
私は、各列の株式の行の値に基づいて複数の基準をテストするために示したループと同じ種類のループを実行します。私のコードでは、.ixを使ってデータフレーム内の「セル」をループしています。 しかし、私は一度に行全体を操作する方法を探しましたが、すべての試みに失敗しました。
これは3000株で約7分かかっています(ただし2000株では約1分ほどかかります)。しかし、これははるかに高速に実行する必要がありますか?
def piotrosky():
df_temp = pd.DataFrame(np.nan, index=range(10), columns=df.columns)
#bruger dictionary til rename input så man ikke skal gøre det for hver række
dic={0:'positiveNetIncome',1:'positiveOperatingCF',2:'increasingROA', 3:'QualityOfEarnings',4:'longTermDebtToAssets',
5:'currentRatio', 6:'sharesOutVsSharesLast',7:'increasingGrossM',8:'IncreasingAssetTurnOver', 9:'total' }
df_temp.rename(dic, inplace = True)
r=1
#df is a vector with stocks in the columns and datapoints in the rows
#so I always need to loop across the columns
for i in range(df.shape[1]-1):
#positive net income
if df.ix[2,r]>0:
df_temp.ix[0,r]=1
else:
df_temp.ix[0,r]=0
#positiveOpeCF
if df.ix[3,r]>0:
df_temp.ix[1,r]=1
else:
df_temp.ix[1,r]=0
#Continue with several simular loops
#total
df_temp.ix[9,r]=df_temp.ix[0,r]+df_temp.ix[1,r]+df_temp.ix[2,r]+df_temp.ix[3,r]+ \
df_temp.ix[4,r]+df_temp.ix[5,r]+df_temp.ix[6,r]+df_temp.ix[7,r]+df_temp.ix[8,r]
r=r+1
Nicolajに列として追加することができます
Series
取得する - '名前変更(列=)'引数として辞書をとり、その代わりに、複数のリネームの、ちょうど使用を'{1: 'positiveNetIncome'、2: 'positiveOperatingCF'、3:...}' – pshep123あなたの質問が形成されるような形で多くの回答を得ることはできません。これを読んでください:https://stackoverflow.com/help/mcve、それに応じてあなたの質問を更新してください。 – pshep123
提案pshep123のおかげで、私はそれに応じて更新しようとします –