-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
900e23e
commit cec2c5d
Showing
3 changed files
with
39 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
|
||
```markdown | ||
@author jackzhenguo | ||
@desc | ||
@desc category列转数值 | ||
@tag | ||
@version | ||
@date 2020/03/18 | ||
``` | ||
|
||
第 180 个小例子:**category列转数值** | ||
|
||
某列取值只可能为有限个枚举值,往往需要转为数值,使用get_dummies,或自己定义函数: | ||
|
||
```python | ||
pd.get_dummies(df['a']) | ||
``` | ||
|
||
自定义函数,结合 apply: | ||
|
||
```python | ||
def c2n(x): | ||
if x=='A': | ||
return 95 | ||
if x=='B': | ||
return 80 | ||
|
||
df['a'].apply(c2n) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
|
||
```markdown | ||
@author jackzhenguo | ||
@desc | ||
@desc rank排名 | ||
@tag | ||
@version | ||
@date 2020/03/19 | ||
``` | ||
|
||
第 181 个小例子:**rank排名** | ||
|
||
rank 方法,生成数值排名,ascending 为False,考试分数越高,排名越靠前: | ||
|
||
```python | ||
In [36]: df = pd.DataFrame({'a':[46, 98,99, 60, 43]} )) | ||
In [53]: df['a'].rank(ascending=False) | ||
Out[53]: | ||
0 4.0 | ||
1 2.0 | ||
2 1.0 | ||
3 3.0 | ||
4 5.0 | ||
``` |