如何重新排列数据集? [英] How To Rearrange Data Set?

查看:136
本文介绍了如何重新排列数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新排列当前如下所示的数据集:

I am trying to rearrange my data set which currently looks like this:

对此:

我一直在手动转置并添加每个源的值,并尝试使用Python自动执行此任务.有人会帮助我朝正确的方向开始吗?

I have been manually transposing and adding up the values for each source and trying my hand at using Python to automate this task. Would anybody be so kind to help me get started in the right direction?

推荐答案

这是潜在的熊猫解决方案.

Here's a potential pandas solution.

我制作了一个测试csv文件.没有逐字复制您的内容,但结构在那里

I made a test csv file. Didn't copy yours verbatim but the structure is there

test_data.csv

test_data.csv

Source,10/30/2017,10/31/2017,11/1/2017,11/2/2017,11/3/2017,11/4/2017,11/5/2017
A,10,11,12,13,14,15,16
B,15,16,17,18,19,20,21
C,20,21,22,23,24,25,26
A,25,26,27,28,29,30,31
B,30,31,32,33,34,35,36
C,35,36,37,38,39,40,41

这是可根据需要处理数据的python代码

Here's the python code to process the data as you want it

import pandas as pd
df = pd.read_csv('test_data.csv')
df = df.set_index('Source')
ser = df.unstack('Source')
ser = ser.groupby(level=[0, 1]).sum()
ser = ser.sort_index(level=1)

此时,ser看起来像这样

            Source
10/30/2017  A         35
10/31/2017  A         37
11/1/2017   A         39
11/2/2017   A         41
11/3/2017   A         43
11/4/2017   A         45
11/5/2017   A         47
10/30/2017  B         45
10/31/2017  B         47
11/1/2017   B         49
11/2/2017   B         51
11/3/2017   B         53
11/4/2017   B         55
11/5/2017   B         57
10/30/2017  C         55
10/31/2017  C         57
11/1/2017   C         59
11/2/2017   C         61
11/3/2017   C         63
11/4/2017   C         65
11/5/2017   C         67
dtype: int64

除此以外,其他任何操作都只能将其格式化并保存为您喜欢的内容.

Anything else beyond this would be formatting and saving it to your liking.

希望这会有所帮助.

这篇关于如何重新排列数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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