Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

pandas - can't merge on index. tried using join, merge,

Tried combining the to dataframes on index using the methods below

df_result = pd.concat([df1,df2])

pd.merge(df2, df1, left_index=True,right_index=True)

df2.join(df1)

But it's not working, nothing is merged.

Is it because I converted the series to a dataframe df1. Do I have to set index somehow on the df1 ?

df1

    signal
6        1
7       -1
8       -1
9       -1
10      -1
11       1

df2

   my_id  
0  638BB   
1  639AA   
2  sddsf   
3  233dd  
4  rere3   
5  erre3  
6  jjk54   
7  sdg88 
8  sdfd2  
9  sdfd3

expected result = df_result

   my_id  signal
0  638BB   NaN
1  639AA   NaN
2  sddsf   NaN
3  233dd   NaN
4  rere3   NaN
5  erre3   NaN
6  jjk54    1
7  sdg88   -1
8  sdfd2   -1
9  sdfdf.  -1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...