
pandas中join()方法很神奇,join()方法虽然是连接方法,但是因为pandas有两个数据结构,join()方法针对两个数据结构有着不同的连接方法:1、根据指定的分隔符将Series中的各个元素的字符串连接起来。通过索引或指定列来连接DataFrame。
方式一:根据指定的分隔符将Series中的各个元素的字符串连接起来。
1 2 3 4 5 6 7 8 9 10 11 12 | import pandas as pd
s = pd.Series([ 'A' , 'B' , 'C' , 'Aaba' , 'Baca' , 'CABA' , 'dog' , 'cat' ])
print (s.str.join( "," ))
0 A
1 B
2 C
3 A,a,b,a
4 B,a,c,a
5 C,A,B,A
6 d,o,g
7 c,a,t
dtype: object
|
方式二:通过索引或指定列来连接DataFrame。
1 2 3 4 5 6 7 | '' '可以通过将两边的key进行set_index' ''
df_set_index = df_AA.set_index( 'zh' ).join(df_ZZ.set_index( 'en' ),how= 'outer' ,
lsuffix= '_A' ,rsuffix= '_Z' )
'' '或设置后边df中key,通过on与指定的左边df中的列进行合并,返回的index不变' ''
df_set_index_on = df_AA.join(df_ZZ.set_index( 'en' ), on= 'zh' , how= 'outer' ,
lsuffix= '_A' ,rsuffix= '_Z' )
|
以上就是pandas中join()的两种应用方法,其中Series中使用join()怒视很常用,但是也要了解下哦~
(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)