脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python pandas 列转行操作详解(类似hive中explode方法)

Python pandas 列转行操作详解(类似hive中explode方法)

2020-05-19 09:58geekingLi Python

这篇文章主要介绍了Python pandas 列转行操作详解(类似hive中explode方法),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

最近在工作上用到Python的pandas库来处理excel文件,遇到列转行的问题。找了一番资料后成功了,记录一下。

1. 如果需要爆炸的只有一列:

?
1
2
3
4
5
6
df=pd.DataFrame({'A':[1,2],'B':[[1,2],[1,2]]})
df
Out[1]:
 A  B
0 1 [1, 2]
1 2 [1, 2]

如果要爆炸B这一列,可以直接用explode方法(前提是你的pandas的版本要高于或等于0.25)

?
1
2
3
4
5
6
7
df.explode('B')
 
  A B
 0 1 1
 1 1 2
 2 2 1
 3 2 2

2. 如果需要爆炸的有2列及以上

?
1
2
3
4
5
6
df=pd.DataFrame({'A':[1,2],'B':[[1,2],[3,4]],'C':[[1,2],[3,4]]})
df
Out[592]:
 A  B  C
0 1 [1, 2] [1, 2]
1 2 [3, 4] [3, 4]

则可以用写一个方法,如下代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def unnesting(df, explode):
 idx = df.index.repeat(df[explode[0]].str.len())
 df1 = pd.concat([
  pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
 df1.index = idx
 
 return df1.join(df.drop(explode, 1), how='left')
 
 
unnesting(df,['B','C'])
Out[2]:
 B C A
0 1 1 1
0 2 2 1
1 3 3 2
1 4 4 2

补充知识:pandas:一列分解成多列 series.str.split(',',expand=True);pyspark 一列分解成多列

源shuju

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
question_id       id
0   17576     70391,70394
1   17576  70391,70392,70393,70394
2   17576     70391,70392
3   40430   155032,155033,155034
4   40430 155032,155033,155034,155035
5   40430   155033,155034,155035
6   40430    155032,155035
7   40430    155034,155035
8   40430    155032,155034
9   40430   155032,155034,155035
10  40430    155033,155034
11  40430    155032,155033
12  40430    155033,155035
13  40430   155032,155033,155035

pandas solution

df.join(df['id'].str.split(',',expand=True)

result

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   0  1  2  3
0 70391 70394 None None
1 70391 70392 70393 70394
2 70391 70392 None None
3 155032 155033 155034 None
4 155032 155033 155034 155035
5 155033 155034 155035 None
6 155032 155035 None None
7 155034 155035 None None
8 155032 155034 None None
9 155032 155034 155035 None
10 155033 155034 None None
11 155032 155033 None None
12 155033 155035 None None
13 155032 155033 155035 None

#注意expand=True

df.join(df['id'].str.split(',',expand=True))

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
question_id       id  0  1  2  3
0   17576     70391,70394 70391 70394 None None
1   17576  70391,70392,70393,70394 70391 70392 70393 70394
2   17576     70391,70392 70391 70392 None None
3   40430   155032,155033,155034 155032 155033 155034 None
4   40430 155032,155033,155034,155035 155032 155033 155034 155035
5   40430   155033,155034,155035 155033 155034 155035 None
6   40430    155032,155035 155032 155035 None None
7   40430    155034,155035 155034 155035 None None
8   40430    155032,155034 155032 155034 None None
9   40430   155032,155034,155035 155032 155034 155035 None
10  40430    155033,155034 155033 155034 None None
11  40430    155032,155033 155032 155033 None None
12  40430    155033,155035 155033 155035 None None
13  40430   155032,155033,155035 155032 155033 155035 None
?
1
2
3
4
5
6
pyspark solution
 tdf=df.select(F.split(df.id,',').alias('ss'),'question_id','count_num')
 tdf.sort('question_id').show()
 res=tdf.select(F.explode(tdf.ss).alias('new'),'question_id','count_num')
res.sort('question_id').show()
res.groupBy('question_id','new').sum().sort('question_id').show()

result

Python pandas 列转行操作详解(类似hive中explode方法)

Python pandas 列转行操作详解(类似hive中explode方法)

以上这篇Python pandas 列转行操作详解(类似hive中explode方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/m0_37773338/article/details/103754807

延伸 · 阅读

精彩推荐