Skip to content

Commit

Permalink
180-181
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhenguo committed Dec 5, 2020
1 parent 900e23e commit cec2c5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@
| 177 | [数据分析神器:deepnote](./md/177.md) | deepnote | v1.0 | ⭐️⭐⭐ |
| 178 | [apply 方法去掉特殊字符](./md/178.md) | pandas apply | v1.0 | ⭐️⭐⭐ |
| 179 | [使用map对列做特征工程](./md/179.md) | pandas map | v1.0 | ⭐️⭐⭐ |
| 180 | [category列转数值](./md/180.md) | pandas category | v1.0 | ⭐️⭐⭐ |
| 181 | [rank排名](./md/181.md) | pandas rank | v1.0 | ⭐️⭐⭐ |

### Python 实战

Expand Down
23 changes: 21 additions & 2 deletions md/180.md
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)
```
18 changes: 16 additions & 2 deletions md/181.md
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
```

0 comments on commit cec2c5d

Please sign in to comment.