I am trying to loop through all the Json files(they are all in the same format) in a certain path, and extract specific fields from those Json file, append them together and saved as an csv file.
I can achieve my goal via following code:
import pandas as pd
import os
allfiles = os.listdir('.')
files = [files for files in allfiles if files.endswith('.json')]
mydata=pd.DataFrame()
for filename in files:
#Read Joson File
df = pd.read_json(filename)
df=df.loc[:,['col1','col2', 'col3']].set_index('col1')
mydata=mydata.append(df)
mydata.to_csv('Result.csv')
For example, my original data in two files looks like:
File 1 File 2
col1 col2 col3 col1 col2 col3
A B C D E F
The result file from my code gives me(in the 2nd image), however I want to have a break line between those two files(as my Target table) when I append them together, so what should I add to my code in order to make this happens?
My Result Target
col1 col2 col3 col1 col2 col3
A B C A B C
D E F
D E F
Thanks
df.append(pd.Series([np.nan]), ignore_index = True)