Home > Mobile >  join dataframes with the same index into excel
join dataframes with the same index into excel

Time:02-17

I'm reading a csv and I'm adding a dataframe to it, but I need you to follow the index consecutively, do you know any way to do it? Attached the code

import pandas as pd

df1 = pd.read_csv("test3.csv")

document = {'price': price, 'get_by': get_by, 'name': name, 'company': company, 'link': link, "date":date,"search_position": search_position } 
df2 = pd.DataFrame(document) 

df3 = pd.concat([df1,df2],axis=1)
df3.to_csv('test3.csv', index=False)

As a result:

enter image description here

df1 and df2 samples

enter image description here

CodePudding user response:

It seems that df1 has a numbering column (the first column) while df2 doesn't.

The solution is to drop the first column in df1 and use

df3.to_csv('test3.csv', index=True)

CodePudding user response:

My solution was the following :

df3.index = range(0,len(df3))

With this line, i create a new index for df3 and it looks like this...

Output: enter image description here

  • Related