Looked at other similar questions, but none has the use case I have. I have multiple files with the same format and no header
file1
id, value
1, 100
2, 150
...
file2
10, 500
11, 510
....
I would like to "merge" them to have
id, value
1, 100
2, 150
...
10, 500
11, 510
...
tried merge, append, concat but couldn't achieve the end result I am looking for.
df2 = pd.DataFrame(columns=['id','value'])
df2.columns = ['id','value']
for file_name in os.listdir(work_dir):
df1 = pd.read_csv(work_dir+'/'+file_name, header=None)
df1.columns = ['id','value']
df2 = pd.merge(df2,df1, on ='id')
Or any other suggestion to load multiple files in to a data frame appreciated. I do have another data frame coming from a db will be merged same way so merge is a question too.
You are looking for append. Try this:
df1.append(df2)
concat
should also work for you. https://pandas.pydata.org/pandas-docs/stable/merging.html
You should also name each column upon import...
pd.read_csv(work_dir+'/'+file_name, header=None, names=['id', 'value'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.