3

I have three dataframes that I would like to crop, I have defined a function;

def croping(data, start_date='2017-04-10 00:00:00', end_date='2018-05-31 21:55:00' ):
    return data.loc[start_date:end_date]

I know this is a bit extra but I am trying to learn how to use user-defined functions.

I then want to use this function on the list of dataframes;

df_list = [df1, df2, df3]

where

df1= 
Timestamp                A          B          C          D          E                        
2017-04-01 00:00:00  106.46451   98.94002  118.59085  100.83779  108.89098
2017-04-01 00:05:00  105.74346   98.93000  113.47805   86.77218  105.37943
2017-04-01 00:10:00  105.99000   99.15727  115.48461   96.76406  106.55555
2017-04-01 00:15:00  105.04311   98.93000  112.15814   88.38959 104.71931
...                  ...         ...       ...         ...

etc.

I am then trying to run a for loop to crop each dataframe

for name in df_list:
    holding = croping(name)

if I do it this way I need to then append the holding dataframes together, is there a way that I can call the cropped dataframe a different thing in each iteration? Something like this;

for name in df_list:
    name_cropped = croping(name)

where name changes in each iteration, so I am left with df1_cropped, df2_cropped etc.

Maybe the best way to do this is not with a for loop, I am still very much learning

jpp
  • 159,742
  • 34
  • 281
  • 339
Luka Vlaskalic
  • 445
  • 1
  • 3
  • 19

2 Answers2

3

You have a couple of options. You can index your dataframes by location in a list. In this case, you can use a list comprehension. Using pd.DataFrame.pipe would be the Pandorable method as it facilitates method chaining:

df_list = [df1, df2, df3]
df_croped = [df.pipe(croping) for df in df_list]

However, you may find it cleaner to use dictionaries instead:

df_dict = dict(enumerate((df1, df2, df3), 1))
df_croped = {k: v.pipe(croping) for k, v in df_dict.items()}

You can then access the first dataframe original or uncropped, via df_dict[0] or df_croped[0] respectively.

jpp
  • 159,742
  • 34
  • 281
  • 339
0

One way is to use 2 lists in place of one i.e

df_list = [df1, df2, df3]
df_list_updated = []

for name in df_list:
    df_list_updated.append(croping(name))
amit thakur
  • 110
  • 1
  • 8