最好的方法是将以下列拆分为一个数据框,该数据框包含每个国家/地区的名称,而另两列包含第一列的数据(历史记录)?
从此数据帧:
+-----------------------------------------+----------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------------------------------+
| coordinates | country | country_code | history | latest | province |
|-----------------------------------------+----------------------------------+----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------------------------------|
| {'lat': '15', 'long': '101'} | Thailand | TH | {'1/22/20': 0, '1/23/20': 0, '1/24/20': 0, '1/25/20': 0, '1/26/20': 0, '1/27/20': 0, '1/28/20': 0, '1/29/20': 0, '1/30/20': 0, '1/31/20': 0, '2/1/20': 0, '2/10/20': 0, '2/11/20': 0, '2/12/20': 0, '2/13/20': 0, '2/14/20': 0, '2/15/20': 0, '2/16/20': 0, '2/17/20': 0, '2/18/20': 0, '2/19/20': 0, '2/2/20': 0, '2/20/20': 0, '2/21/20': 0, '2/22/20': 0, '2/23/20': 0, '2/24/20': 0, '2/25/20': 0, '2/26/20': 0, '2/27/20': 0, '2/28/20': 0, '2/29/20': 0, '2/3/20': 0, '2/4/20': 0, '2/5/20': 0, '2/6/20': 0, '2/7/20': 0, '2/8/20': 0, '2/9/20': 0, '3/1/20': 1, '3/10/20': 1, '3/11/20': 1, '3/12/20': 1, '3/13/20': 1, '3/14/20': 1, '3/15/20': 1, '3/16/20': 1, '3/2/20': 1, '3/3/20': 1, '3/4/20': 1, '3/5/20': 1, '3/6/20': 1, '3/7/20': 1, '3/8/20': 1, '3/9/20': 1} | 1 | |
| {'lat': '36', 'long': '138'} | Japan | JP | {'1/22/20': 0, '1/23/20': 0, '1/24/20': 0, '1/25/20': 0, '1/26/20': 0, '1/27/20': 0, '1/28/20': 0, '1/29/20': 0, '1/30/20': 0, '1/31/20': 0, '2/1/20': 0, '2/10/20': 0, '2/11/20': 0, '2/12/20': 0, '2/13/20': 1, '2/14/20': 1, '2/15/20': 1, '2/16/20': 1, '2/17/20': 1, '2/18/20': 1, '2/19/20': 1, '2/2/20': 0, '2/20/20': 1, '2/21/20': 1, '2/22/20': 1, '2/23/20': 1, '2/24/20': 1, '2/25/20': 1, '2/26/20': 2, '2/27/20': 4, '2/28/20': 4, '2/29/20': 5, '2/3/20': 0, '2/4/20': 0, '2/5/20': 0, '2/6/20': 0, '2/7/20': 0, '2/8/20': 0, '2/9/20': 0, '3/1/20': 6, '3/10/20': 10, '3/11/20': 15, '3/12/20': 16, '3/13/20': 19, '3/14/20': 22, '3/15/20': 22, '3/16/20': 27, '3/2/20': 6, '3/3/20': 6, '3/4/20': 6, '3/5/20': 6, '3/6/20': 6, '3/7/20': 6, '3/8/20': 6, '3/9/20': 10} | 27 |
到这个:
country days values
Thailand 1/2/22 0
Thailand 2/2/22 0
Thailand 2/2/22 0
....
Sweden 3/4/55 0
Sweden 3/4/55 0
问题来源:stackoverflow
IIUC,
new_df = (pd.DataFrame(df['history'].tolist(),
index = df['country'])
.reset_index()
.melt('country',var_name = 'days')
.sort_values('country'))
或暗示:
#import numpy as np
pd.DataFrame(data = np.concatenate([[(k, v) for k, v in d.items()]
for d in df['history']]),
columns = ['days','values'],
index = df['country'].repeat(df['history'].str.len())).reset_index()
例
print(df)
country country_code history
0 A 0 {1: 0, 2: 0}
1 B 1 {1: 0, 2: 0}
2 C 2 {1: 0, 2: 0}
*
new_df = (pd.DataFrame(df['history'].tolist(), index = df['country']) .reset_index() .melt('country',var_name = 'days',value_name='values') .sort_values('country')) print(new_df) country days values 0 A 1 0 3 A 2 0 1 B 1 0 4 B 2 0 2 C 1 0 5 C 2 0
也许第二种方法更好
%%timeit
pd.DataFrame(data = np.concatenate([[(k,v) for k,v in d.items()]
for d in df['history']]),
columns = ['days','values'],
index = df['country'].repeat(df['history'].str.len())).reset_index()
1.71 ms ± 137 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
*
%%timeit new_df = (pd.DataFrame(df['history'].tolist(), index = df['country']) .reset_index() .melt('country',var_name = 'days') .sort_values('country')) new_df 5.01 ms ± 272 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。