We will Come across this case when we have to analyze data from different csv files.
The task is to read and concatenate all csv files in same directory.
First we should import numpy,pandas,os libraries
The Following code will list the all the csv file iin the directory
import pandas as pd
import numpy as np
import os
files = [file for file in os.listdir(r'C:\Users\sarad\1DataScience\Pandas-Data-Science-Tasks-master\SalesAnalysis\Sales_Data')]
for i in files:
print(i)
with the following code we get final concatenated dataframe
for file in files:
df = pd.read_csv('./Pandas-Data-Science-Tasks-master/SalesAnalysis/Sales_Data/'+file) #adding file name at the end of the path
result_df = pd.concat([result_df,df]) #concatenating previous and current data Frame
result_df.head()
Final Output:
Code:
The task is to read and concatenate all csv files in same directory.
First we should import numpy,pandas,os libraries
The Following code will list the all the csv file iin the directory
import pandas as pd
import numpy as np
import os
files = [file for file in os.listdir(r'C:\Users\sarad\1DataScience\Pandas-Data-Science-Tasks-master\SalesAnalysis\Sales_Data')]
for i in files:
print(i)
with the following code we get final concatenated dataframe
for file in files:
df = pd.read_csv('./Pandas-Data-Science-Tasks-master/SalesAnalysis/Sales_Data/'+file) #adding file name at the end of the path
result_df = pd.concat([result_df,df]) #concatenating previous and current data Frame
result_df.head()
Final Output:
Code:
0 Comments