Revanth Tv
Revanth Tv

Reputation: 53

How to combine two columns from different csv files to one csv file

I have seen many methods like concat, join, merge but i am missing the technique for my simple dataset. I have two datasets looks like mentioned below

dates.csv
2020-07-06
2020-07-07
2020-07-08
2020-07-09
2020-07-10
.....
...
...

mydata.csv
Expected,Predicted

12990,12797.578628473471

12990,12860.382061836583

12990,12994.159035827917

12890,13019.073929662367

12890,12940.34108357684
.............
.......
.....

I want to combine these two datasets which have same number of rows on btoh csv files. I tried concat method but i see NaN's

delete = dates.csv (pd.DataFrame)
data1 = mydata.csv (pd.DataFrame)
result = pd.concat([delete, data1], axis=0, ignore_index=True)
print(result)
Output: 
              0  Expected     Predicted
0    2020-07-06       NaN           NaN
1    2020-07-07       NaN           NaN
2    2020-07-08       NaN           NaN
3    2020-07-09       NaN           NaN
4    2020-07-10       NaN           NaN
..          ...       ...           ...
307         NaN   10999.0  10526.433098
308         NaN   10999.0  10911.247147
309         NaN   10490.0  11038.685328
310         NaN   10490.0  10628.204624
311         NaN   10490.0  10632.495169
[312 rows x 3 columns]

I dont want all NaN's.

Thanks for your help!

Upvotes: 0

Views: 640

Answers (2)

Armaaj
Armaaj

Reputation: 33

If your two dataframes respect the same order, you can use the join method mentionned by Nik, by default it joins on the index.

Otherwise, if you have a key that you can join your dataframes on, you can specify it like this:

joined_data = first_df.join(second_df, on=key)

Your first_df and second_df should then share a column with the same name to join on.

Upvotes: 0

Nik
Nik

Reputation: 113

You could use .join() method from pandas.

delete = dates.csv (pd.DataFrame)
data1 = mydata.csv (pd.DataFrame)

result = delete.join(data1) 

Upvotes: 1

Related Questions