How to create a new dataframe with every iteration of for loop in Python

后端 未结 1 1269
渐次进展
渐次进展 2021-01-16 06:38

Click here to see image

#### the data is inverted #######

#### To bring back to its original position ####### 
   df_1= df_i.iloc[::-1]

#### Set index a         


        
相关标签:
1条回答
  • 2021-01-16 07:23

    You should make up your question so that other people will get max advantage from this site.

    I will try to propose solution to this question.

    On every iteration I want create a new data frame, How?

    The idea is to store the dataframes as values of a dictionary.

    cnt = 22  # your loop
    dict_of_df = {} # initialize empty dictionary
    
    for i in range(0,22):
        newname = df_sheetnames['col'].values[i]
        dict_of_df["df_{}".format(i)] = pd.read_excel('DATA.xlsx', sheetname=newname, skiprows=6, usecols=[14,15,16])
    

    You can access the dataframes by call dict_of_df[key], where key = "df_1", "df_2", ... , "df_22"

    Now you have many dataframes and you want to concat, use pandas.concat()

    If you want to rename the columns after that, simply write complete_df.columns = ['col1', 'col2', 'col3', ...]

    0 讨论(0)
提交回复
热议问题