提交 3304d2fc 编写于 作者: F feilong

fix bug

上级 38edf626
{
"source": "stat.py",
"depends": [],
"exercise_id": 125,
"type": "code_options"
"author": "huanhuilong",
"source": "stat.md",
"depends": [],
"exercise_id": 125,
"type": "code_options"
}
\ No newline at end of file
# 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.
先完成此消息的编辑!
想要评论请 注册