python little dian: merge or join

Keywords: Big Data

Merge (): Pay attention to using merge or join
# merge is because two merged objects have the same columns, and merge merges with these same columns as indexes. Merge by index, two sorce s will appear, not join

# join implements index-by-index merging regardless of whether they have the same columns. At the same time, you can specify on = keyname', then link the two DFS according to the index, and reset the index of 0,1,2..

#ValueError: columns overlap but no suffix specified: Index(['sorce'], dtype='object')
#Merge is because two merged objects have the same columns, and merge merges with these same columns as indexes.
df_new= pd.merge(df,df_dummies3)
print(df_new)
print(df_new.columns)
 
----------------
  data1    key  sorce  data1_a  data1_b  data1_c  key_blue  key_green  key_red
0     a  green     33      1.0      0.0      0.0       0.0        1.0      0.0
1     b    red     61      0.0      1.0      0.0       0.0        0.0      1.0
2     c   blue     99      0.0      0.0      1.0       1.0        0.0      0.0
Index(['data1', 'key', 'sorce', 'data1_a', 'data1_b', 'data1_c', 'key_blue',
       'key_green', 'key_red'],
      dtype='object')
 
----------------------
#Merge is not allowed here, two columns are not the same. pandas.tools.merge.MergeError: No common columns to perform merge on
#join implements merging by index, regardless of whether they have the same column or not. At the same time, you can specify on = keyname', then link two DFS according to index, and reset the index of 0,1,2..
df_new= df.join(df_dummies6)
print(df_new)
print(df_new.columns)
-------------------
data1    key  sorce  sorce_Fail,  sorce_pass  sorce_excellent
0     a  green     33        1.0       0.0       0.0
1     b    red     61        0.0       1.0       0.0
2     c   blue     99        0.0       0.0       1.0
Index(['data1', 'key', 'sorce', 'sorce_Fail,', 'sorce_pass', 'sorce_excellent'], dtype='object')

Author: shuihupo
Source: CSDN
Details can be found in the original: https://blog.csdn.net/shuihupo/article/details/82717654.
Copyright Statement: This article is the original article of the blogger. Please attach a link to the blog article for reprinting.

Posted by iceomnia on Mon, 28 Jan 2019 21:42:15 -0800