ahmadsq95
ahmadsq95

Reputation: 1

Load CSV files in dictionary then make data frame for each csv file Python

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?

Upvotes: 0

Views: 334

Answers (4)

greybeard
greybeard

Reputation: 2467

When there is no processing of data from one data set/frame involving another data set, don't collect data sets.
Just process the current one, and proceed to the next.

The conventional name for a variable receiving an "unpacked value" not going to be used is _:

for _, df in csvDict.items():
    df['Cat_Frames'] = df['Cat_Frames'].apply(process).apply(…

- but why ask for keys to ignore them? Iterate the values:

for df in csvDict.values():
    df['Cat_Frames'] = df['Cat_Frames'].apply(process).apply(…

Upvotes: 0

cstout
cstout

Reputation: 11

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

Upvotes: 1

Nathan Mills
Nathan Mills

Reputation: 2279

You can chain the applys like this:

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

Upvotes: 1

Ayush Paine
Ayush Paine

Reputation: 41

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

I think this is how you should traverse the dictionary.

Upvotes: 0

Related Questions