Python Forum
Concatenate/Join/Merge two Dataframes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Concatenate/Join/Merge two Dataframes
#1
Hi,

I'm trying to concatenate two Dataframes with the same size but doesn't seem to work. any ideas? links to images
- my Dataframes: https://www.imageupload.net/image/VbexE

result = pd.concat([test_df, pred_df], axis=1).reindex(test_df.index)
result
- result: https://www.imageupload.net/image/VbhW5

Thks
Reply
#2
What are you wanting to do? One source dataframe has a date column, the other does not. What behavior are you expecting from the attempt at merge?
Reply
#3
(Jan-20-2020, 04:26 PM)jefsummers Wrote: What are you wanting to do? One source dataframe has a date column, the other does not. What behavior are you expecting from the attempt at merge?

Hi,
the first source is the test dataset and the second one is the prediction (predicted values, that is why there is no data column) that I have saved in a dataframe. I want to merge both and have a dataframe like the one in the second image I posted.
About the date column ... is there any tip/trick I can use here.
Reply
#4
Totally unclear for me what data you have and how you want to manipulate it.

Want to add column to existing dataframe?

>>> df = pd.DataFrame({'nums': range(5)})                            
>>> df                                                               
   nums
0     0
1     1
2     2
3     3
4     4
>>> df['chars'] = list('abcde')                                      
>>> df                                                               
   nums chars
0     0     a
1     1     b
2     2     c
3     3     d
4     4     e
>>> df['NaNs'] = np.nan                                              
>>> df                                                               
   nums chars  NaNs
0     0     a   NaN
1     1     b   NaN
2     2     c   NaN
3     3     d   NaN
4     4     e   NaN
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Jan-21-2020, 11:04 AM)perfringo Wrote: Totally unclear for me what data you have and how you want to manipulate it.

Want to add column to existing dataframe?

>>> df = pd.DataFrame({'nums': range(5)})                            
>>> df                                                               
   nums
0     0
1     1
2     2
3     3
4     4
>>> df['chars'] = list('abcde')                                      
>>> df                                                               
   nums chars
0     0     a
1     1     b
2     2     c
3     3     d
4     4     e
>>> df['NaNs'] = np.nan                                              
>>> df                                                               
   nums chars  NaNs
0     0     a   NaN
1     1     b   NaN
2     2     c   NaN
3     3     d   NaN
4     4     e   NaN

Hi,

I have two Dataframes.
- test_df with 2 columns: date as index and night_cons (see image)
- pred_df with just a column "prediction" (see image)

I'm trying to merge both but when I do it, the final merged Dataframe gives NAN in the prediction column (see image please)

Hi,
I got it ... It was an index issue.
Thank you all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate 3D arrays paul18fr 1 2,589 Apr-09-2021, 02:49 PM
Last Post: paul18fr
  Merging two DataFrames based on indexes from two other DataFrames lucinda_rigeitti 0 1,724 Jan-16-2020, 08:36 PM
Last Post: lucinda_rigeitti
  concatenate mcgrim 1 2,195 Mar-22-2019, 01:31 PM
Last Post: buran
  Why can't I merge pandas dataframes learnpython2018 2 7,619 Sep-23-2018, 05:53 PM
Last Post: learnpython2018

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020