Read Multiple Csv Files Into Dataframe Python
Read Multiple CSV Files & Suspend into 1 pandas DataFrame in Python (Instance)
In this tutorial, I'll explicate how to import multiple CSV files and combine them into a unmarried pandas DataFrame in Python.
The page contains these contents:
It'south fourth dimension to dive into the exemplifying Python code!
Case Data & Improver Libraries
If we want to utilise the functions of the pandas library, nosotros get-go take to load pandas:
import pandas as pd # Load pandas
import pandas as pd # Load pandas
Next, we'll likewise demand to construct some data that we tin can use in the case below:
data1 = pd.DataFrame ( { 'x1':range ( 1 , vii ) , # Create kickoff pandas DataFrame 'x2':[ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ] , 'x3':range ( 7 , 1 , - ane ) } ) print (data1) # Print outset pandas DataFrame
data1 = pd.DataFrame({'x1':range(1, 7), # Create first pandas DataFrame 'x2':['a', 'b', 'c', 'd', 'east', 'f'], 'x3':range(vii, i, - 1)}) print(data1) # Print first pandas DataFrame
data2 = pd.DataFrame ( { 'x1':range ( xi , 17 ) , # Create 2nd pandas DataFrame 'x2':[ 'x' , 'y' , 'y' , '10' , 'y' , 'x' ] , 'x3':range ( 17 , 11 , - one ) } ) impress (data2) # Print 2nd pandas DataFrame
data2 = pd.DataFrame({'x1':range(11, 17), # Create second pandas DataFrame 'x2':['10', 'y', 'y', 'x', 'y', 'x'], 'x3':range(17, 11, - 1)}) print(data2) # Impress second pandas DataFrame
data3 = pd.DataFrame ( { 'x1':range ( 101 , 107 ) , # Create 3rd pandas DataFrame 'x2':[ 'q' , 'w' , 'eastward' , 'e' , 'w' , 'q' ] , 'x3':range ( 107 , 101 , - 1 ) } ) impress (data3) # Print tertiary pandas DataFrame
data3 = pd.DataFrame({'x1':range(101, 107), # Create third pandas DataFrame 'x2':['q', 'w', 'e', 'east', 'due west', 'q'], 'x3':range(107, 101, - 1)}) print(data3) # Impress third pandas DataFrame
As shown in Tables one, 2, and 3, the previous Python programming syntax has constructed three pandas DataFrames. Each of these DataFrames contains the same column names, but different values.
Allow'due south export these DataFrames to dissimilar CSV files:
data1.to_csv ( 'data1.csv' , index = Imitation ) # Export pandas DataFrames to three CSVs data2.to_csv ( 'data2.csv' , index = False ) data3.to_csv ( 'data3.csv' , index = Imitation )
data1.to_csv('data1.csv', index = False) # Export pandas DataFrames to three CSVs data2.to_csv('data2.csv', alphabetize = False) data3.to_csv('data3.csv', index = False)
After we take executed the previous Python code, iii new CSV files are appearing in our electric current working directory. These CSV files will exist used as a footing for the following example.
Instance: Import Multiple CSV Files & Concatenate into I 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 nosotros 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 list of CSV file names
In the next stride, we tin use a for loop to read and join all our data sets into a unmarried 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 step, though.
data_all = pd.concat ( (pd.read_csv (i) for i in file_names) ).reset_index (drib = Truthful ) # 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 Table iv – We accept created a new pandas DataFrame that contains all the rows in our iii input CSV files.
Video, Further Resources & Summary
Take a look at the following video on my YouTube aqueduct. In the video, I'm explaining the contents of this article in a programming session.
The YouTube video will be added soon.
In addition, yous might want to read the other tutorials which I have published on this website.
- Read CSV File as pandas DataFrame in Python
- Read CSV File without Unnamed Alphabetize Column
- Append pandas DataFrame to Existing CSV File
- Suspend pandas DataFrame in Python
- Suspend Rows to pandas DataFrame in Loop
- Reindex & Reset Index of pandas DataFrame from 0
- Introduction to Python
To summarize: In this Python tutorial you have learned how to read several CSV files and combine them into a single pandas DataFrame. In case you lot have any additional questions, please let me know in the comments below.
Source: https://statisticsglobe.com/read-multiple-csv-files-python
Postar um comentário for "Read Multiple Csv Files Into Dataframe Python"