0

I have a dataframe as below

my_df = pd.DataFrame({
    'Imp':  [1,2,3,4,5,6,7,8,9,0],
    'Apple':  ['a','b','c','d','e','f','g','h','i','j']
})
print(my_df)
  Imp   Apple
0   1   a
1   2   b
2   3   c
3   4   d
4   5   e
5   6   f
6   7   g
7   8   h
8   9   i
9   0   j

Then i have a function as below. The intention of this function is to slice the original df based on some logic. Logic is not important here & the function runs fine.

The problem what i am facing is that i am not able to assign the sliced pieces of dataframe are assigned to the name 'df1','df2' ...

I have put in dfi = df.loc[0:i,] but this i am doing something incorrect this this very line & i am not able to figure out what.

Expected output is when I print (df1), i should get the first slice of that was created using this function. Can anyone help?

def func(df):
    i=1
    while len(df)!=0:
        dfi=df.loc[0:i,]
        df=df.loc[i:,].reset_index(drop=True)
        i=i+1
        if len(df)==0:
            break 
moys
  • 7,747
  • 2
  • 11
  • 42

2 Answers2

2

Make a dictionary where you will have sliced df's,

something similar to code given below

def func(df, d = {}):
    i=1
    while len(df)!=0:
        d[f"df{i}"] = df.loc[0:i,]
        ...
        i+=1

Output:

d = {
"df1": sliced df1,
"df2": sliced df2,
...
}
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • It is not working if `d = {}` is inside the function. If it is outside. it does. You may want to change your answer. Is there no way i can assign the sliced dataframes to string 'df1' or 'df2' directly in the loop? – moys Oct 17 '19 at 04:58
  • @moys It should work, even it is inside a function(until unless you are calling it recursively). Update the code. Please check now. – shaik moeed Oct 17 '19 at 05:01
  • @moys In dictionary, you are assigning *the sliced dataframes to string 'df1' or 'df2' directly in the loop* as a `key`. – shaik moeed Oct 17 '19 at 05:05
1

This could do it.This assigns to values to df1, df2 variables as you have mentioned

def func(df):
    i=1
    while len(df)!=0:
        globals()["df"+str(i)]=df.loc[0:i,]
        df=df.loc[i:,].reset_index(drop=True)
        i=i+1
        if len(df)==0:
            break 
ArunJose
  • 1,999
  • 1
  • 10
  • 33