Set pandas names after list items

pandas dataframe
pandas set column names from list
pandas set row names
pandas column to list
pandas column names
pandas list
pandas dataframe operations
change column name pandas

I have multiple xlsx files with data in it that i want to import to separate dataframes in Python.

Currently, i want to import a spreadsheet from 2014 september and 2014 october. The spreadsheets are in different folders for each year and the file names are as follows in the month_list variable. The desired dataframe names are "2014_09_sept_df" and "2014_10_okt_df"

month_list = ['09_sept', '10_okt']

df_string_list=[]
k = 0

for i in range(2014,2015):
    dirstring = 'C:/Users/folder/' + str(i)
    os.chdir(dirstring)

    for j in month_list:
        filestring = dirstring + '/' + j + '.xlsx'
        my_file = Path(filestring)            

        if my_file.exists():
            df_string_list.append(str(i) + '_' + j + '_df')
            df_string_list[k] = pd.read_excel(filestring)
            k+=1              

My problem is that I've created the df_string_list list for holding the dataframe names, and when I want to name the dataframes with that item from the list, the dataframe gets put into the list with this command, instead of creating a dataframe:

df_string_list[k] = pd.read_excel(filestring)

If I understand you correctly, I suggest you use a dict and not a list:

df_string_list={}    #create an empty dict

for i in range(2014,2015):
    dirstring = 'C:/Users/folder/' + str(i)
    os.chdir(dirstring)

    for j in month_list:
        filestring = dirstring + '/' + j + '.xlsx'
        my_file = Path(filestring)            

        if my_file.exists():
            df_string_list[str(i) + '_' + j + '_df'] = pd.read_excel(filestring)

in this case, for example, df_string_list['2014_09_sept_df'] contains the Dataframe from the right file.

To know all dataframe names loaded in your dict, use:

for dfname in df_string_list.keys():
  print(dfname)
  #print(df_string_list[dfname]    #uncomment to print the desired df

Convert pandas.DataFrame, Series and list to each other, pandas.DataFrame,pandas.Series and Python's built-in type list can be Series , you can set and change the row and column names by Related: Transpose 2D list in Python (swap rows and columns) Series , but it is easier to set the index with the set_index() method after reading the whole list. Is possible to change the names using data in the above list? My original data set has 100 columns and I did not want to do it manually for each column. I was trying the following using df.rename but keep getting errors.

If you want to use a list element as your dataframe name, you can do something like this at the end of your loop

globals()[df_string_list[k]] = pd.read_excel(filestring) 

Create a Pandas DataFrame from Lists, Let's see how can we create a Pandas DataFrame from Lists. Output: Code #2: Dataframe using list with index and column names Calling DataFrame constructor after zipping DataFrame based on the existing columns � How to create an empty DataFrame and append rows & columns to it in Pandas? Python list is easy to work with and also list has a lot of in-built functions to do a whole lot of operations on lists. Pandas dataframe’s columns consist of series but unlike the columns, Pandas dataframe rows are not having any similar association.

So the problem was fixed. :) I can't use a list item, to name a dataframe, but the exec function and .format saved the day, like this:

if my_file.exists():
            exec('{} = pd.DataFrame(pd.read_csv(filestring))'.format(j + '_' + str(i)))

Additional note: I also realized, that a dataframe name can't start with a number.

pandas.Index.set_names — pandas 1.1.0 documentation, Set Index or MultiIndex name. Able to set new names partially and by level. Parameters. nameslabel or list of label. Name(s) to set. levelint, label or list of int or� How to get column names in Pandas dataframe 05-12-2018 While analyzing the real datasets which are often very huge in size, we might need to get the column names in order to perform some certain operations.

pandas.DataFrame.isin — pandas 1.1.0 documentation, Whether each element in the DataFrame is contained in values. If values is a dict, the keys must be the column names, which must match. When values is a list check whether every value in the DataFrame is present in the list (which� pandas.Series.isin¶ Series.isin (values) [source] ¶ Whether elements in Series are contained in values.. Return a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.

pandas.Series.loc — pandas 1.1.0 documentation, A list or array of labels, e.g. ['a', 'b', 'c'] . df.loc['viper'] max_speed 4 shield 5 Name: viper, dtype: int64. List of Set value for all items matching the list of labels. You can use the rename () method of pandas.DataFrame to change any row / column name individually. pandas.DataFrame.rename — pandas 0.22.0 documentation Specify the original name and the new name in dict like {original name: new name} to index / columns of rename (). index is for index name and columns is for the columns name.

pandas.DataFrame.rename_axis — pandas 1.1.0 documentation, Set the name of the axis for the index or columns. Parameters. mapperscalar, list- like, optional. Value to This parameter only apply for DataFrame type objects. Python Pandas : How to get column and row names in DataFrame; Python: Add column to dataframe in Pandas ( based on other column or list or default value) Python Pandas : Drop columns in DataFrame by label Names or by Index Positions; Pandas : Loop or Iterate over all or certain columns of a dataframe

Comments
  • Great! This worked and created a dictionary, that contains my needed dataframes. How can I convert this dictionary to separate dataframes, as it was intended in the beginning?
  • you have the different keys in the dictionary which are the name of dataframe,for example df_string_list['2014_09_sept_df'] is one of dataframe in the dictionary. You have all keys and dataframe linked . If i missunderstand you explain precisely in the question