def inverted_index_test(): df1 = pd.DataFrame({"A":['a c a','b d'],"B":['a d a','c a']}) print(df1) print("A分割操作") df2 = df1['A'].str.split(' ',expand=True) print(df2) print("堆叠操作") df3 = df2.stack() print(df3) print("修剪索引") df4 = df3.reset_index(level=1,drop=True)#level 会删除对应的mutil值 df4 = pd.DataFrame(df4) print(df4.index) print(df4) print("执行join操作") df5 = df4.join(df1) print(df5)
输出内容:
Python 3.9.12 (main, Apr 5 2022, 06:56:58) [GCC 7.5.0] on linux A B 0 a c a a d a 1 b d c a A分割操作 0 1 2 0 a c a 1 b d None 堆叠操作 0 0 a 1 c 2 a 1 0 b 1 d dtype: object 修剪索引 Int64Index([0, 0, 0, 1, 1], dtype='int64') 0 0 a 0 c 0 a 1 b 1 d 执行join操作 0 A B 0 a a c a a d a 0 c a c a a d a 0 a a c a a d a 1 b b d c a 1 d b d c a
标签:index,join,倒排,方式,df4,df1,索引,print From: https://www.cnblogs.com/chenjie0949/p/17054722.html