简体   繁体   English

在python中重塑表格

[英]Reshaping a table in python

Being new to Python I am finding it a little difficult to comprehend solutions to problems of 'similar' (not sure though) nature posted on the forum and all my attempts to correlate them with mine have not been successful 我是Python的新手,我发现很难理解论坛上发布的“相似”(虽然不确定)性质问题的解决方案,而我将它们与我的关联的所有尝试均未成功

I have a .csv file (part of which) shown below 我有一个.csv文件(部分内容如下所示)

Rank Day Parameter 10 1 x_time 10 2 x_time 9 3 x_time 2 15 x_time 7 16 x_time 10 18 x_time 3 25 x_time 5 31 x_time 2 35 x_time 4 1 t_msg 3 5 t_msg 5 9 t_msg 8 10 t_msg 4 90 t_msg 8 4 g_up 3 5 g_up 3 56 g_up

Problem Statement: The .csv file has been extracted from a dataset; 问题陈述:.csv文件已从数据集中提取; the aim of which is to study the pattern of the "parameter" over a span of period (say 90 days) along with its "rank"(gravity) on any given "day" of a period. 目的是研究一段时间(例如90天)内“参数”的模式及其在任何给定“天”内的“等级”(重力)。 The said parameter may or may not occur on a particular day during the said period. 所述参数可以在所述期间的特定日期出现或不出现。

A model now exists where every instance of occurrence of a parameter is being put in a separate row (of the csv file). 现在存在一个模型,其中,参数出现的每个实例都放在(csv文件的)单独的行中。 What I am now attempting (in vain,till) is that for every unique parameter only one row may exist with 90 corresponding columns for each day(as the analysis window is 90 days). 我现在正在尝试(徒劳地)是,对于每个唯一参数,每天只有一行存在90条对应的列(因为分析窗口为90天)。 For all days when the parameter is ranked, its ranking comes in column as it is and the others are left as 0. 对于参数排名的所有日子,其排名都会按原样显示在列中,而其他则保留为0。

If may explanation has aided confusion let me put it in this way. 如果可能的解释助长了混乱,让我这样说。 Considering the csv file had been as shown could I reshape this to a one having 3 rows and 90 columns 考虑到csv文件已经如图所示,我可以将其重塑为一个具有3行90列的文件

          day1   day2   day3    day4  ............ day90 
x_time     10     10    9        0                   0 
t_msg       4      0    0        0                   4
g_up        0      0    0        8                   0
  .
  .
  .

PS:I understand that I have been mentioning .csv file all throughout but have put 'table' in the title as I'm made to believe that such a feat may only be accomplished through a table. PS:我了解到我一直都在提到.csv文件,但在标题中加上了“表格”,因为我相信这样的壮举只能通过表格来完成。 Please correct me if wrong. 如果有错,请纠正我。

Thanking in anticipation 期待中的感谢

I did this with a dictionary of 90-element lists. 我用90个元素列表的字典来做到这一点。 The parameter serves as the dictionary key; 该参数用作字典键; the day is the index into the list. 这一天是列表中的索引。 You stuff the rank into that list location. 您将等级填充到该列表位置。

my_table = {
    "x_time": 90*[0],
    "t_msg": 90*[0],
    "g_up": 90*[0],
}

with open("p.csv") as f:
    for line in f:
        r, d, p = line.split(',')
        rank = int(r)
        day = int(d)
        param = p.strip()
        my_table[param][day-1] = rank

print my_table

... and here's the output on your given data (which I put into p.csv): ...这是您给定数据(我放入p.csv中)的输出:

{'x_time': [10, 10, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 7, 0, 10, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
'g_up': [0, 0, 0, 8, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
't_msg': [4, 0, 0, 0, 3, 0, 0, 0, 5, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM