Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_python
提交
3304d2fc
S
skill_tree_python
项目概览
CSDN 技术社区
/
skill_tree_python
通知
66
Star
14
Fork
6
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
3304d2fc
编写于
11月 25, 2021
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bug
上级
38edf626
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
120 addition
and
1 deletion
+120
-1
data/3.python高阶/2.结构化数据分析工具Pandas/4.高级应用/stat.json
data/3.python高阶/2.结构化数据分析工具Pandas/4.高级应用/stat.json
+2
-1
data/3.python高阶/2.结构化数据分析工具Pandas/4.高级应用/stat.md
data/3.python高阶/2.结构化数据分析工具Pandas/4.高级应用/stat.md
+118
-0
未找到文件。
data/3.python高阶/2.结构化数据分析工具Pandas/4.高级应用/stat.json
浏览文件 @
3304d2fc
{
{
"source"
:
"stat.py"
,
"author"
:
"huanhuilong"
,
"source"
:
"stat.md"
,
"depends"
:
[],
"depends"
:
[],
"exercise_id"
:
125
,
"exercise_id"
:
125
,
"type"
:
"code_options"
"type"
:
"code_options"
...
...
data/3.python高阶/2.结构化数据分析工具Pandas/4.高级应用/stat.md
0 → 100644
浏览文件 @
3304d2fc
# pandas dataframe之apply
apply的使用
## template
```
python
import
pandas
as
pd
import
numpy
as
np
def
add_val
(
num
):
if
num
>
0
:
return
1
else
:
return
0
if
__name__
==
'__main__'
:
data
=
{
'cloumn_one'
:
pd
.
Series
(
np
.
random
.
randint
(
-
10
,
10
,
size
=
5
)),
'cloumn_two'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
)),
'cloumn_three'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
))
}
data_df
=
pd
.
DataFrame
(
data
)
data_df
[
'label_one'
]
=
data
[
'cloumn_one'
].
apply
(
add_val
)
print
(
data_df
)
```
## 答案
```
python
import
pandas
as
pd
import
numpy
as
np
if
__name__
==
'__main__'
:
data
=
{
'cloumn_one'
:
pd
.
Series
(
np
.
random
.
randint
(
-
10
,
10
,
size
=
5
)),
'cloumn_two'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
)),
'cloumn_three'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
))
}
data_df
=
pd
.
DataFrame
(
data
)
data_df
[
'label_one'
]
=
data
[
'cloumn_one'
].
apply
[
'num'
>
1
]
print
(
data_df
)
```
## 选项
### A
```
python
import
pandas
as
pd
import
numpy
as
np
def
add_val
(
num
):
if
num
>
0
:
return
1
else
:
return
0
if
__name__
==
'__main__'
:
data
=
{
'cloumn_one'
:
pd
.
Series
(
np
.
random
.
randint
(
-
10
,
10
,
size
=
5
)),
'cloumn_two'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
)),
'cloumn_three'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
))
}
data_df
=
pd
.
DataFrame
(
data
)
data_df
[
'label_one'
]
=
data
[
'cloumn_one'
].
apply
(
add_val
)
print
(
data_df
)
```
### B
```
python
import
pandas
as
pd
import
numpy
as
np
if
__name__
==
'__main__'
:
data
=
{
'cloumn_one'
:
pd
.
Series
(
np
.
random
.
randint
(
-
10
,
10
,
size
=
5
)),
'cloumn_two'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
)),
'cloumn_three'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
))
}
data_df
=
pd
.
DataFrame
(
data
)
data_df
[
'label_one'
]
=
data
[
'cloumn_one'
].
apply
(
lambda
num
:
1
if
num
>
0
else
0
)
print
(
data_df
)
```
### C
```
python
import
pandas
as
pd
import
numpy
as
np
if
__name__
==
'__main__'
:
data
=
{
'cloumn_one'
:
pd
.
Series
(
np
.
random
.
randint
(
-
10
,
10
,
size
=
5
)),
'cloumn_two'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
)),
'cloumn_three'
:
pd
.
Series
(
np
.
random
.
randint
(
0
,
10
,
size
=
5
))
}
data_df
=
pd
.
DataFrame
(
data
)
def
add_val
(
num
):
return
1
if
num
>
0
else
0
data_df
[
'label_one'
]
=
data
[
'cloumn_one'
].
apply
(
add_val
)
print
(
data_df
)
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录