最近在工作上用到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方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“Python pandas 列转行操作详解(类似hive中explode方法)”评论...
更新日志
2024年10月07日
2024年10月07日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]