橙色-通过基于列值创建新行来重写数据



我正在尝试处理新冠肺炎病例数据

(来源,仅供参考:https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv)

这些数据形成一个矩阵,按列列出日期,按行列出国家/地区。简化视图:

country 1/20/20 1/21/20 1/22/20 ... etc. ...
China   100     120     144     ... etc. ...
US      0       0       1       ... etc. ...
...
etc.
...

我正试图将日期列和数字转换为两个新功能,比如"日期"one_answers"已确认",例如:

country date     confirmed
China   1/20/20  100
China   1/21/20  120
China   1/22/20  144
US      1/20/20  0
US      1/21/20  0
US      1/22/20  1
...  etc.  ...

我对任何嵌入Orange的解决方案都感兴趣,当然,我们可以在导入之前准备数据!

您是在脚本中还是在画布中执行此操作?用(纯(画布,我想你做不到。当然,您可以始终使用Python脚本小部件并在那里进行操作。

在脚本(独立的或画布内(中,您应该将Orange.data.Table视为不可变的,尽管Orange本身并没有强制执行。一些版本支持可以更改行数的过时方法,这些方法已被删除。你仍然可以更改数据,但我不建议这样做。

您必须创建一个从一开始就具有适当大小的新表。我想最简单的方法是收集Python列表中所需的所有数据,然后将其传递给Table.from_list

披露:我是Orange的开发人员之一,我正在写一篇使用这些数据的博客文章。这将是一个系列,我们也会在一两周内展示一些这样的剧本。

Janez的回答正好提供了我需要的信息,谢谢。

在这里,我用Orange发布了python脚本的代码,以给出该问题的完整解决方案。

from Orange.data import Domain, TimeVariable, ContinuousVariable, StringVariable, Table
# toISODate: parse the date string and returns it in ISO format
def toISODate(s):
bits = s.split('/')
year = '20'+bits[2];
day = bits[1]
day = day if len(day)==2 else '0'+day
month = bits[0]
month = month if len(month)==2 else '0'+month
return year+'-'+month+'-'+day
# Prepare output domain
tmVar = TimeVariable.make("Date");
out_domain = Domain([ContinuousVariable.make("Lat"),
ContinuousVariable.make("Long"),
tmVar,
ContinuousVariable.make("Count")],
[],
[StringVariable.make("Country"),
StringVariable.make("Region")]
)
# Prepare output source - list of lists
out_source = []
in_domain = in_data.domain # not really needed
in_width = len(in_domain) # oops - thank you @community for the reminder
# outer loop through original data rows
for in_row in in_data:
lat, long = in_row[0], in_row[1] # grab latitute, longitude
country, region = in_row.metas[1], in_row.metas[0]
# inner loop through dates in each row
for j in range(2,in_width):
date = tmVar.parse(toISODate(in_domain[j].name)) # find date (in clolumn names), parse to UNIX
# make new row: lat, long, date, number of cases, country, region
out_row = [lat, long, date, in_row[j], country, region]
out_source.append(out_row)
# end
# end in_row
# Make data from the list
out_data = Table.from_list(out_domain,out_source)

理想情况下,应该编写代码来处理任何矩阵(将列x、行y、值转换为具有三列x、y、值的高表;然后处理补充列(。我的Python还不能胜任。

此外,可以将内部循环中的"in_width"值替换为"in_data.domain.attributes"。这样做将确保循环覆盖不断增加的列数(每天一个新列(

最新更新