将多行数据合并为一列 [英] Combine data in many row into a columnn

查看:56
本文介绍了将多行数据合并为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据:

  year  Male
1 2011    8
2 2011    1
3 2011    4
4 2012    3
5 2012    12
6 2012    9
7 2013    4
8 2013    3
9 2013    3

我需要将2011年的数据归为一列,将2012年的数据归为下一列,依此类推上。

and I need to group the data for the year 2011 in one column, 2012 in the next column and so on.

   2011  2012  2013
1   8     3     4 
2   1    12     3
3   4     9     3

如何实现?

推荐答案

如果每个年的行数相同,则一个选项是 unstack

One option is unstack if the number of rows per 'year' is the same

unstack(df1, Male ~ year)

这篇关于将多行数据合并为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆