1

im new to Python, and have what is probably a basis question.

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.

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.

For example for one DF i do:

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:

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.

Hope you can give a small example as how to do this.

2
  • How do you import your dataframes ? Commented Dec 23, 2016 at 14:45
  • Create a function to perform these actions on a dataframe and rerun the result to the mail function. then you can call this function to for multiple dataframes Commented Dec 23, 2016 at 15:00

2 Answers 2

10

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help! Very much appreciated! This solution is exactly what I was looking for!
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.