将COVID-19 JH数据透视到时间序列行

时间:2020-04-01 18:15:27

标签: python pandas formatting

我正在尝试透视Johns Hopkins数据,以使日期列为行,而其余信息保持不变。前七个列应为列,而其余列(日期列)应为行。任何帮助将不胜感激。

加载和过滤数据

import pandas as pd
import numpy as np
deaths_url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_US.csv'
confirmed_url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_US.csv'

dea = pd.read_csv(deaths_url)
con = pd.read_csv(confirmed_url)

dea = dea[(dea['Province_State'] == 'Texas')]
con = con[(con['Province_State'] == 'Texas')]

查看数据的新近度并进行数据透视

# get the most recent data of data
mostRecentDate = con.columns[-1] # gets the columns of the matrix

# show the data frame
con.sort_values(by=mostRecentDate, ascending = False).head(10)

# save this index variable to save the order.
index = data.columns.drop(['Province_State']) 

# The pivot_table method will eliminate duplicate entries from Countries with more than one city
data.pivot_table(index = 'Admin2', aggfunc = sum)

# formatting using a variety of methods to process and sort data
finalFrame = data.transpose().reindex(index).transpose().set_index('Admin2').sort_values(by=mostRecentDate, ascending=False).transpose()

结果数据框如下所示,但是它没有保留任何日期时间

enter image description here

我也尝试过:

date_columns = con.iloc[:, 7:].columns
con.pivot(index = date_columns, columns = 'Admin2', values = con.iloc[:, 7:])
ValueError: Must pass DataFrame with boolean values only

编辑: 按照指导,我尝试了第一个答案中列出的melt命令,它不创建日期行,只是删除了所有其他非日期值。

date_columns = con.iloc[:, 7:].columns
con.melt(id_vars=date_columns)

最终结果应如下所示:

  Date  iso2    iso3    code3   FIPS    Admin2  Province_State  Country_Region  Lat Long_   Combined_Key
1/22/2020   US  USA 840 48001   Anderson    Texas   US  31.81534745 -95.65354823    Anderson, Texas, US
1/22/2020   US  USA 840 48003   Andrews Texas   US  32.30468633 -102.6376548    Andrews, Texas, US
1/22/2020   US  USA 840 48005   Angelina    Texas   US  31.25457347 -94.60901487    Angelina, Texas, US
1/22/2020   US  USA 840 48007   Aransas Texas   US  28.10556197 -96.9995047 Aransas, Texas, US

1 个答案:

答案 0 :(得分:0)

使用pandas melt。很好的例子here

示例:

In [41]: cheese = pd.DataFrame({'first': ['John', 'Mary'],
   ....:                        'last': ['Doe', 'Bo'],
   ....:                        'height': [5.5, 6.0],
   ....:                        'weight': [130, 150]})
   ....: 

In [42]: cheese
Out[42]: 
  first last  height  weight
0  John  Doe     5.5     130
1  Mary   Bo     6.0     150

In [43]: cheese.melt(id_vars=['first', 'last'])
Out[43]: 
  first last variable  value
0  John  Doe   height    5.5
1  Mary   Bo   height    6.0
2  John  Doe   weight  130.0
3  Mary   Bo   weight  150.0

In [44]: cheese.melt(id_vars=['first', 'last'], var_name='quantity')
Out[44]: 
  first last quantity  value
0  John  Doe   height    5.5
1  Mary   Bo   height    6.0
2  John  Doe   weight  130.0
3  Mary   Bo   weight  150.0

根据您的情况,您需要在一个数据框(即confinalframe或日期列所在的任何位置)上进行操作。例如:

con.melt(id_vars=date_columns)

请参阅具体示例here