Python Tutorial: Reading multiple data files

Publié le: 10 mars 2020
sur la chaîne: DataCamp
26,274
181

Want to learn more? Take the full course at https://learn.datacamp.com/courses/me... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---

Welcome to "Merging DataFrames with Pandas". My name is Dhavide Aruliah. I'm an applied mathematician and data scientist.

This course is all about merging and combining DataFrames for your data science needs.

Your data rarely exists as DataFrames from the outset: you generally have to deal with text files, spreadsheets and databases.

Let's first check out how to read multiple files into a collection of DataFrames.

The primary tool we've used for data import is read_csv().

This function accepts the filepath of a comma-separated values file as input and returns a Pandas DataFrame directly.

read_csv() has about fifty optional calling parameters permitting very fine-tuned data import.

Pandas has other convenient tools (with similar default calling syntax) that import various data formats like Excel, HTML, or JSON into DataFrames.

To read multiple files using Pandas, we generally need separate DataFrames.

For example, here we call pd dot read_csv() twice to read two CSV files, sales-jan-2015 dot csv and sales-feb-2015 dot csv, into two distinct DataFrames.

It's generally more efficient to iterate over a collection of file names.

With that goal, we can create a list filenames with the two filepaths from before.

We then initialize an empty list called dataframes and iterate through the list filenames.

Within each iteration, we invoke read_csv() to read a DataFrame from a file and we append the resulting DataFrame to the list dataframes.

We can also do the preceding computation with a list comprehension.

Comprehensions are a convenient Python construction for exactly this kind of loop where an empty list is appended to within each iteration.

You can check out DataCamp's Python programming courses for more details on comprehensions.

When many filenames have a similar pattern, the glob module from the Python Standard Library is very useful.

Here, we start by importing the function glob() from the built-in glob module.

We use the pattern sales asterisk dot csv to match any strings that start with prefix sales and end with the suffix dot csv.

The asterisk is a wildcard that matches zero or more standard characters.

The function glob() uses the wildcard pattern to create an iterable object filenames containing all matching filenames in the current directory.

Finally, the iterable filenames is consumed in a list comprehension that makes a list called dataframes containing the relevant data structures.

Now it's your turn to practice reading multiple files into DataFrames.

#PythonTutorial #Python #DataCamp #Reading #multiple #data #Merging #DataFrames #pandas


Sur cette page du site, vous pouvez voir la vidéo en ligne Python Tutorial: Reading multiple data files durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur DataCamp 10 mars 2020, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 26,274 fois et il a aimé 181 téléspectateurs. Bon visionnage!