python - 在熊猫数据框中插入缺失的类别和日期
问题描述
我有以下数据框。我想为每个组(a、b、c、d)、所有日期(有两个日期 - 2020-06-01 和 2020-06-02)添加所有分数级别(高、中、低)
x = pd.DataFrame(data={ 'date' : ['2020-06-01','2020-06-01','2020-06-02','2020-06-01','2020-06-02','2020-06-01','2020-06-02','2020-06-02','2020-06-02'],
'group' : ['a','a','a','b','b','c','c','c','d'],
'score' : ['high','low','mid','low','high','high','high','mid','high'],
'count' : [12,13,2,19,22,3,4,49,12]})
我可以添加以下所有科目的分数类别,但我也无法添加日期
cats = ['high', 'mid','low']
x_re = pd.DataFrame(list(product(x['group'].unique(), cats)),columns=['group', 'score'])
x_re.merge(x, how='left').fillna(0)
预期的输出是这样的:所以每个主题有 6 行,每个日期有 3 行,每个分数类别有 1 行。然后用缺少数据点的 np.nan (或零很好)填充计数
pd.DataFrame(data={ 'date' : ['2020-06-01','2020-06-01','2020-06-01','2020-06-02','2020-06-02','2020-06-02','2020-06-01','2020-06-01','2020-06-01','2020-06-02','2020-06-02','2020-06-02','2020-06-01','2020-06-01','2020-06-01','2020-06-02','2020-06-02','2020-06-02','2020-06-01','2020-06-01','2020-06-01','2020-06-02','2020-06-02','2020-06-02'],
'group' : ['a','a','a','a','a','a','b','b','b','b','b','b','c','c','c','c','c','c','d','d','d','d','d','d'],
'score' : ['high','low','mid','high','low','mid','high','low','mid','high','low','mid','high','low','mid','high','low','mid','high','low','mid','high','low','mid'],
'count' : [12, 13, np.nan, np.nan, np.nan, 2, np.nan, 22, np.nan, 19, np.nan, np.nan, 3, np.nan, np.nan, 4, np.nan, np.nan, np.nan, np.nan, np.nan, 12, np.nan, 49]})
任何建议都会很棒,谢谢
解决方案
您的解决方案可以通过唯一值添加列来修改,如果输入数据中date
不是唯一的三元组,此解决方案可以工作:date, group, score
cats = ['high', 'mid','low']
x_re = pd.DataFrame(list(product(x['date'].unique(),
x['group'].unique(),
cats)),columns=['date','group', 'score'])
x = x_re.merge(x, how='left').fillna(0)
与reindex
by3 level MultiIndex
的解决方案类似:
cats = ['high', 'mid','low']
x_re = pd.MultiIndex.from_product([x['date'].unique(),
x['group'].unique(),
cats],names=['date','group', 'score'])
x = x.set_index(['date','group','score']).reindex(x_re).reset_index()
print (x)
date group score count
0 2020-06-01 a high 12.0
1 2020-06-01 a mid NaN
2 2020-06-01 a low 13.0
3 2020-06-01 b high NaN
4 2020-06-01 b mid NaN
5 2020-06-01 b low 19.0
6 2020-06-01 c high 3.0
7 2020-06-01 c mid NaN
8 2020-06-01 c low NaN
9 2020-06-01 d high NaN
10 2020-06-01 d mid NaN
11 2020-06-01 d low NaN
12 2020-06-02 a high NaN
13 2020-06-02 a mid 2.0
14 2020-06-02 a low NaN
15 2020-06-02 b high 22.0
16 2020-06-02 b mid NaN
17 2020-06-02 b low NaN
18 2020-06-02 c high 4.0
19 2020-06-02 c mid 49.0
20 2020-06-02 c low NaN
21 2020-06-02 d high 12.0
22 2020-06-02 d mid NaN
23 2020-06-02 d low NaN
一次调用unstack
和一次调用stack
是可能的,但所有唯一值cats
都必须存在于输入数据中是必要的:
x = (x.set_index(['date', 'group', 'score'])
.unstack(['group','score'])
.stack([1, 2], dropna=False)
.reset_index())
print (x)
date group score count
0 2020-06-01 a high 12.0
1 2020-06-01 a low 13.0
2 2020-06-01 a mid NaN
3 2020-06-01 b high NaN
4 2020-06-01 b low 19.0
5 2020-06-01 b mid NaN
6 2020-06-01 c high 3.0
7 2020-06-01 c low NaN
8 2020-06-01 c mid NaN
9 2020-06-01 d high NaN
10 2020-06-01 d low NaN
11 2020-06-01 d mid NaN
12 2020-06-02 a high NaN
13 2020-06-02 a low NaN
14 2020-06-02 a mid 2.0
15 2020-06-02 b high 22.0
16 2020-06-02 b low NaN
17 2020-06-02 b mid NaN
18 2020-06-02 c high 4.0
19 2020-06-02 c low NaN
20 2020-06-02 c mid 49.0
21 2020-06-02 d high 12.0
22 2020-06-02 d low NaN
23 2020-06-02 d mid NaN
推荐阅读
- javascript - 使用智能支付按钮(React + Redux)的 PayPal 结帐创建订单问题
- node.js - 无法安装 Npx
- excel - 宏工作簿在执行过程中自动关闭
- image-processing - 将多边形缩小为一条线
- swift - 如何区分我的类型与模块类型
- python-3.x - Remove rows in a 2d numpy array with opposite elements
- python - 计算素数的数量直到给定数量
- javascript - 动画原生延迟加载图像(loading='“lazy”)
- java - 如何使用 Spring 提供的 Kafka api 在消费组中创建多个消费者
- excel - Excel - 有 BUT 函数吗?