Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8fbfe43

Browse filesBrowse files
author
binzhouchn
committed
update on 20210914
1 parent 049b798 commit 8fbfe43
Copy full SHA for 8fbfe43

File tree

Expand file treeCollapse file tree

1 file changed

+26
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+26
-1
lines changed
Open diff view settings
Collapse file

‎01_basic/README.md‎

Copy file name to clipboardExpand all lines: 01_basic/README.md
+26-1Lines changed: 26 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
[**44. 调试神器 - 丢弃print**](#调试神器)
9090

91+
[**45. 分组计算均值并填充**](#分组计算均值并填充)
92+
9193
---
9294
<details close>
9395
<summary>点击展开</summary>
@@ -1148,7 +1150,30 @@ if os.environ['pysnooper'] == '0':
11481150
return wrapper
11491151
```
11501152

1151-
###
1153+
### 分组计算均值并填充
1154+
1155+
```python
1156+
def pad_mean_by_group(df, gp_col='stock_id'):
1157+
# 只留下需要处理的列
1158+
cols = [col for col in df.columns if col not in["stock_id", "time_id", "target", "row_id"]]
1159+
# 查询nan的列
1160+
df_na = df[cols].isna()
1161+
# 根据分组计算平均值
1162+
df_mean = df.groupby(gp_col)[cols].mean()
1163+
1164+
# 依次处理每一列
1165+
for col in cols:
1166+
na_series = df_na[col]
1167+
names = list(df.loc[na_series,gp_col])
1168+
1169+
t = df_mean.loc[names,col]
1170+
t.index = df.loc[na_series,col].index
1171+
1172+
# 相同的index进行赋值
1173+
df.loc[na_series,col] = t
1174+
return df
1175+
train_pca = pad_mean_by_group(train_pca)
1176+
```
11521177

11531178

11541179

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.