Victor Uwaje
Victor Uwaje

Reputation: 53

A function with for loop and a function call

I have a function as below:

def get_type(df, index):
    df_type = df[index] #so i can obtain for index 0, 1, 2.. until n-1
    return df_type

I would like to create a for loop in a function that loop throught the index and appends them in a list, like:

def all_df_list(df, index):
    #where index is n-1 
    list_of_df = []
    list_of_columns = []
    for i in range(index):
        df = get_type(df, i)
        list_of_df.append(df)
        list_of_df_colums(df.columns[0])

So that I can get the list of all the index df and their columns with :

get_df_and_columns = all_df_list(df, index=50)
#say we have 51 items in the df

How can I tackle something like this?

Upvotes: 0

Views: 53

Answers (1)

julian
julian

Reputation: 471

assuming you have a dataframe df and want its index and columns as lists

def all_df_list(df):
    col = df.columns.to_list()
    index = df.index.to_list()
    return (col, index)

Upvotes: 1

Related Questions