循环遍历不同的 Pandas 数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41303365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through different Pandas Dataframes
提问by Nicolaj Jeppesen
im new to Python, and have what is probably a basis question.
我是 Python 新手,有什么可能是基础问题。
I have imported a number of Pandas Dataframes consisting of stock data for different sectors. So all columns are the same, just with different dataframe names.
我导入了许多 Pandas Dataframes,其中包含不同行业的股票数据。所以所有列都是相同的,只是数据框名称不同。
I need to do a lot of different small operations on some of the columns, and I can figure out how to do it on one Dataframe at a time, but I need to figure out how to loop over the different frames and do the same operations on each.
我需要对某些列执行许多不同的小操作,并且我可以弄清楚如何一次在一个 Dataframe 上执行此操作,但是我需要弄清楚如何遍历不同的帧并执行相同的操作在各个。
For example for one DF i do:
例如对于我做的一个 DF:
ConsumerDisc['IDX_EST_PRICE_BOOK']=1/ConsumerDisc['IDX_EST_PRICE_BOOK']
ConsumerDisc['IDX_EST_EV_EBITDA']=1/ConsumerDisc['IDX_EST_EV_EBITDA']
ConsumerDisc['INDX_GENERAL_EST_PE']=1/ConsumerDisc['INDX_GENERAL_EST_PE']
ConsumerDisc['EV_TO_T12M_SALES']=1/ConsumerDisc['EV_TO_T12M_SALES']
ConsumerDisc['CFtoEarnings']=ConsumerDisc['CASH_FLOW_PER_SH']/ConsumerDisc['TRAIL_12M_EPS']
And instead of just copying and pasting this code for the next 10 sectors, I want to to do it in a loop somehow, but I cant figure out how to access the df via variable, eg:
而不是仅仅为接下来的 10 个扇区复制和粘贴此代码,我想以某种方式在循环中执行此操作,但我无法弄清楚如何通过变量访问 df,例如:
CS=['ConsumerDisc']
CS['IDX_EST_PRICE_BOOK']=1/CS['IDX_EST_PRICE_BOOK']
so I could just create a list of df names and loop through it.
所以我可以创建一个 df 名称列表并循环遍历它。
Hope you can give a small example as how to do this.
希望你能举一个小例子来说明如何做到这一点。
回答by Roger Thomas
You're probably looking for something like this
你可能正在寻找这样的东西
for df in (df1, df2, df3):
df['IDX_EST_PRICE_BOOK']=1/df['IDX_EST_PRICE_BOOK']
df['IDX_EST_EV_EBITDA']=1/df['IDX_EST_EV_EBITDA']
df['INDX_GENERAL_EST_PE']=1/df['INDX_GENERAL_EST_PE']
df['EV_TO_T12M_SALES']=1/df['EV_TO_T12M_SALES']
df['CFtoEarnings']=df['CASH_FLOW_PER_SH']/df['TRAIL_12M_EPS']
Here we're iterating over the dataframes that we've put in a tuple datasctructure, does that make sense?
在这里,我们正在迭代我们放入元组数据结构中的数据帧,这有意义吗?
回答by gold_cy
Do you mean something like this?
你的意思是这样的吗?
import pandas as pd
d = {'a' : pd.Series([1, 2, 3, 10]), 'b' : pd.Series([2, 2, 6, 8])}
z = {'d' : pd.Series([4, 2, 3, 1]), 'e' : pd.Series([21, 2, 60, 8])}
df = pd.DataFrame(d)
zf = pd.DataFrame(z)
df.head()
a b
0 1 2
1 2 2
2 3 6
3 10 8
df = df.apply(lambda x: 1/x)
df.head()
a b
0 1.0 0.500000
1 2.0 0.500000
2 3.0 0.166667
3 10.0 0.125000
You have more functions so you can create a function and then just apply
that to each DataFrame. Alternatively you could also apply these lambda functions to only specific columns. So lets say you want to apply only 1/column to the every column but the last (going by your example, I am assuming it is in the end) you could do df.ix[:, :-1].apply(lambda x : 1/x)
.
您有更多的功能,因此您可以创建一个功能,然后将apply
其添加到每个 DataFrame 中。或者,您也可以将这些 lambda 函数仅应用于特定列。因此,假设您只想将 1/column 应用于每一列,但最后一个(根据您的示例,我假设它在最后)您可以执行df.ix[:, :-1].apply(lambda x : 1/x)
.