明霞山资源网 Design By www.htccd.com

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

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

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)

df.explode('B')
 
  A B
 0 1 1
 1 1 2
 2 2 1
 3 2 2

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

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]

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

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

 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

   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))

 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
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方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
Python,pandas,列转行,hive,explode

明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?