2

I have 2/ more csv files in folder and I want to import those files to different data frame and want to rename data frame as per file name.

import os
import pandas as pd

redShiftKeyFolderPath = r'D:\Sunil_Work\temp8\Prod_IMFIFSS2017' # contains 2 csv files i.e Prod_IMFIFSS2017_Indicator.csv & Prod_IMFIFSS2017_Location.csv

def importRedshiftKeys(redShiftKeyFolderPath):
    for file in os.listdir(redShiftKeyFolderPath):
        if file.endswith('.csv'):
            redShiftKey = pd.read_csv(os.path.join(redShiftKeyFolderPath, file), dtype = object) # importing

            i = file.rfind('_'); file = file[i:]; rDfName = 'redShiftKey_' + file.replace('_', '').replace('.csv', '') # taking last part of the file name after _ & excluding .csv
            print('Need to rename dataframe as: ', rDfName)

            # Here i want to rename dataframe: "redShiftKey" with new name stored in "rDfName"
    return

importRedshiftKeys(redShiftKeyFolderPath)

I expect 2 data frame i.e redShiftKey_Indicator & redShiftKey_Location

Learnings
  • 2,780
  • 9
  • 35
  • 55
  • and what is your question? – Jean-François Fabre Aug 15 '17 at 08:08
  • @Jean-François Fabre, rename data frame name dynamically – Learnings Aug 15 '17 at 08:09
  • 2
    If you want to create two dataframes, each with the same name as the csv files then you should just do it manually. `fname1 = pd.read_csv('fname1.csv')` `fname2 = pd.read_csv('fname2.csv')` – Legend_Ari Aug 15 '17 at 08:13
  • and what is the problem? error? not working? maybe you should [edit] your code, semicolons make the "interestint" line hard to read. – Jean-François Fabre Aug 15 '17 at 08:13
  • Possible duplicate of [generating variable names on fly in python](https://stackoverflow.com/questions/4010840/generating-variable-names-on-fly-in-python) – IanS Aug 15 '17 at 08:15
  • @ Jean-François Fabre, there is no error in code, since folder has 2 files and my code will read both the file and store in dataFrame: redShiftKey both the times in same dataframe. I want to rename redShiftKey as name contains in variable rDfName, I need 2 separate data frame. File: Prod_IMFIFSS2017_Indicator.csv should store in redShiftKey_Indicator and Prod_IMFIFSS2017_Location.csv store in redShiftKey_Location. I want to do change dataframe name on fly. – Learnings Aug 15 '17 at 08:23
  • @Legend_Ari, I have n numbers of csv files in different folder, So I want to do change data frame name on fly. – Learnings Aug 15 '17 at 08:24
  • @IanS, its not duplicate, your link helps for column name, but i wanted for data frame name change on fly. – Learnings Aug 15 '17 at 08:44
  • Perhaps consider using the [Xarray](http://xarray.pydata.org/en/stable/examples/quick-overview.html#pandas) package, where the name can be assigned as an attribute for your DataArray – Legend_Ari Aug 15 '17 at 08:59

2 Answers2

3

You can use a dictionary, where each key holds a dataframe as a value:

import os
import pandas as pd

redShiftKeyFolderPath = r'D:\Sunil_Work\temp8\Prod_IMFIFSS2017' # contains 2 csv files i.e Prod_IMFIFSS2017_Indicator.csv & Prod_IMFIFSS2017_Location.csv

def importRedshiftKeys(redShiftKeyFolderPath):
    data = {}
    for file in os.listdir(redShiftKeyFolderPath):
        if file.endswith('.csv'):
            csv_file_name = file 

            i = file.rfind('_'); file = file[i:]; rDfName = 'redShiftKey_' + file.replace('_', '').replace('.csv', '') # taking last part of the file name after _ & excluding .csv
            data[rDfName] = redShiftKey = pd.read_csv(os.path.join(redShiftKeyFolderPath, csv_file_name), dtype = object) # importing

    return data

importRedshiftKeys(redShiftKeyFolderPath)

For creating a variable dynamically, you need to check this discussion: generating variable names on fly in python

However, a dictionary strategy is better because you can handle a dynamic n number of csv files, plus your can iterate through them easily:

for Df_name, df in data.items():
    # do any further processing in here 
Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
1

Use globals()[newName] = redShiftKey , this will work

Plinus
  • 308
  • 1
  • 3
  • 10