Home > front end >  Load CSV files in dictionary then make data frame for each csv file Python
Load CSV files in dictionary then make data frame for each csv file Python

Time:03-06

I have multiple csv files I was able to load them as data frames into dictionary by using keywords

# reading files into dataframes
csvDict = {}
for index, rows in keywords.iterrows():
    eachKey = rows.Keyword
    csvFile = "SRT21"   eachKey   ".csv"
    csvDict[eachKey] = pd.read_csv(csvFile)

Now I have other functions to apply on every data frame's specific column.

on a single data frame the code would be like this



df['Cat_Frames'] = df['Cat_Frames'].apply(process)

df['Cat_Frames'] = df['Cat_Frames'].apply(cleandata)

df['Cat_Frames'] = df['Cat_Frames'].fillna(' ')

My question is how to loop through every data frame in the dictionary to apply those function?

I have tried

for item in csvDict.items():
    df = pd.DataFrame(item)
    df

and it gives me empty result

any solution or suggestion?

CodePudding user response:

You can chain the applys like this:

for key, df in csvDict.items():
    df['Cat_Frames'] = df['Cat_Frames'].apply(process).apply(cleandata).fillna(' ')

CodePudding user response:

for key, value in csvDict.items():
   df = pd.DataFrame(value)
   df

I think this is how you should traverse the dictionary.

CodePudding user response:

Items returns a tuple of key/value, so you should make your for loop actually say:

for key, value in csvDict.items():
  print(df)

also you need to print the df if you aren't in jupyter

  • Related