Skip to content
Advertisement

Read multiple csv files with Pandas assign different names

How do I read different csv files in folder without concatenating them but just assigning them with the original file name? For example, file with path …table1.csv will be named “table1”

I managed to get all file names how do I read each file now?

JavaScript

In other words, instead of reading multiple csv files in pandas like this:

JavaScript

is there a better way?

Advertisement

Answer

Use pathlib and dictionary:

JavaScript

Strongly discouraged, prefer method above

If you want to create variables dynamically:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement