How Can You Read Multiple .csv Files From a Same Folder
Read Multiple CSV Files & Append into 1 pandas DataFrame in Python (Example)
In this tutorial, I'll explicate how to import multiple CSV files and combine them into a single pandas DataFrame in Python.
The page contains these contents:
It'due south time to dive into the exemplifying Python lawmaking!
Example Data & Add-On Libraries
If nosotros want to use the functions of the pandas library, nosotros first accept to load pandas:
import pandas every bit pd # Load pandas
import pandas as pd # Load pandas
Next, we'll too need to construct some data that we tin use in the example below:
data1 = pd.DataFrame ( { 'x1':range ( 1 , seven ) , # Create first pandas DataFrame 'x2':[ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] , 'x3':range ( vii , 1 , - ane ) } ) print (data1) # Print beginning pandas DataFrame
data1 = pd.DataFrame({'x1':range(i, 7), # Create first pandas DataFrame 'x2':['a', 'b', 'c', 'd', 'e', 'f'], 'x3':range(vii, 1, - one)}) impress(data1) # Print first pandas DataFrame
data2 = pd.DataFrame ( { 'x1':range ( eleven , 17 ) , # Create second pandas DataFrame 'x2':[ 'x' , 'y' , 'y' , 'ten' , 'y' , 'x' ] , 'x3':range ( 17 , 11 , - i ) } ) print (data2) # Print second pandas DataFrame
data2 = pd.DataFrame({'x1':range(11, 17), # Create 2d pandas DataFrame 'x2':['x', 'y', 'y', 'x', 'y', 'x'], 'x3':range(17, 11, - 1)}) print(data2) # Print second pandas DataFrame
data3 = pd.DataFrame ( { 'x1':range ( 101 , 107 ) , # Create third pandas DataFrame 'x2':[ 'q' , 'w' , 'e' , 'e' , 'westward' , 'q' ] , 'x3':range ( 107 , 101 , - i ) } ) impress (data3) # Impress third pandas DataFrame
data3 = pd.DataFrame({'x1':range(101, 107), # Create third pandas DataFrame 'x2':['q', 'w', 'e', 'e', 'due west', 'q'], 'x3':range(107, 101, - 1)}) print(data3) # Print tertiary pandas DataFrame
As shown in Tables 1, 2, and 3, the previous Python programming syntax has constructed three pandas DataFrames. Each of these DataFrames contains the aforementioned column names, but different values.
Let'due south export these DataFrames to different CSV files:
data1.to_csv ( 'data1.csv' , alphabetize = Imitation ) # Export pandas DataFrames to three CSVs data2.to_csv ( 'data2.csv' , index = Faux ) data3.to_csv ( 'data3.csv' , index = Imitation )
data1.to_csv('data1.csv', index = Simulated) # Export pandas DataFrames to three CSVs data2.to_csv('data2.csv', alphabetize = False) data3.to_csv('data3.csv', index = False)
After we have executed the previous Python code, three new CSV files are actualization in our current working directory. These CSV files will be used as a basis for the following instance.
Example: Import Multiple CSV Files & Concatenate into One pandas DataFrame
The following Python programming syntax shows how to read multiple CSV files and merge them vertically into a single pandas DataFrame.
For this task, we first accept to create a list of all CSV file names that we want to load and append to each other:
file_names = [ 'data1.csv' , 'data2.csv' , 'data3.csv' ] # Create list of CSV file names
file_names = ['data1.csv', 'data2.csv', 'data3.csv'] # Create listing of CSV file names
In the next step, nosotros can use a for loop to read and join all our data sets into a single pandas DataFrame.
Note that I'm also using the reset_index role to reset the index numbers in our concatenated data. This is an optional pace, though.
data_all = pd.concat ( (pd.read_csv (i) for i in file_names) ).reset_index (drop = True ) # Import print (data_all) # Print combined pandas DataFrame
data_all = pd.concat((pd.read_csv(i) for i in file_names)).reset_index(drop = True) # Import print(data_all) # Print combined pandas DataFrame
The output of the previous Python code is shown in Tabular array 4 – Nosotros have created a new pandas DataFrame that contains all the rows in our iii input CSV files.
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. In the video, I'm explaining the contents of this commodity in a programming session.
The YouTube video will be added soon.
In addition, you lot might desire to read the other tutorials which I have published on this website.
- Read CSV File every bit pandas DataFrame in Python
- Read CSV File without Unnamed Index Column
- Append pandas DataFrame to Existing CSV File
- Append pandas DataFrame in Python
- Suspend Rows to pandas DataFrame in Loop
- Reindex & Reset Alphabetize of pandas DataFrame from 0
- Introduction to Python
To summarize: In this Python tutorial you accept learned how to read several CSV files and combine them into a single pandas DataFrame. In example you lot have any additional questions, please let me know in the comments below.
Source: https://statisticsglobe.com/read-multiple-csv-files-python
Enviar um comentário for "How Can You Read Multiple .csv Files From a Same Folder"