Python-Pandas(4)自定义函数方法
来源:互联网 发布:2017淘宝标题优化技巧 编辑:程序博客网 时间:2024/06/05 16:32
#specifying axis=1 or axis='columns' will drop any columns that have null valuesdrop_na_columns = titanic_survival.dropna(axis=1)new_titanic_survival = titanic_survival.dropna(axis=0,subset=["Age", "Sex"])#print new_titanic_survival
row_index_83_age = titanic_survival.loc[83,"Age"]row_index_1000_pclass = titanic_survival.loc[766,"Pclass"]print row_index_83_ageprint row_index_1000_pclass
new_titanic_survival = titanic_survival.sort_values("Age",ascending=False)print new_titanic_survival[0:10]itanic_reindexed = new_titanic_survival.reset_index(drop=True)print(titanic_reindexed.iloc[0:10])
# This function returns the hundredth item from a seriesdef hundredth_row(column): # Extract the hundredth item hundredth_item = column.iloc[99] return hundredth_item# Return the hundredth item from each columnhundredth_row = titanic_survival.apply(hundredth_row)print hundredth_row
def not_null_count(column): column_null = pd.isnull(column) null = column[column_null] return len(null)column_null_count = titanic_survival.apply(not_null_count)print column_null_count
#By passing in the axis=1 argument, we can use the DataFrame.apply() method to iterate over rows instead of columns.def which_class(row): pclass = row['Pclass'] if pd.isnull(pclass): return "Unknown" elif pclass == 1: return "First Class" elif pclass == 2: return "Second Class" elif pclass == 3: return "Third Class"classes = titanic_survival.apply(which_class, axis=1)print classes
def is_minor(row): if row["Age"] < 18: return True else: return Falseminors = titanic_survival.apply(is_minor, axis=1)#print minorsdef generate_age_label(row): age = row["Age"] if pd.isnull(age): return "unknown" elif age < 18: return "minor" else: return "adult"age_labels = titanic_survival.apply(generate_age_label, axis=1)print age_labels
titanic_survival['age_labels'] = age_labelsage_group_survival = titanic_survival.pivot_table(index="age_labels", values="Survived")print age_group_survival
阅读全文
0 0
- Python-Pandas(4)自定义函数方法
- pandas 自定义函数方法
- python pandas 函数累积
- Python-pandas常用函数
- Python pandas.read_csv()函数
- Pandas DataFrame 函数方法
- python pandas numpy matplotlib 常用方法及函数 转载▼ import numpy as np import pandas as pd import matplotlib
- python pandas dataframe 去重函数
- 【Python】Pandas的apply函数使用示例
- python pandas dataframe 去重函数
- Python拓展包:Numpy,pandas...常用函数
- python pandas 聚合与分组函数
- python | pandas | 移动窗口函数rolling
- python学习(九)----pandas模块相关函数
- python pandas dataframe 去重函数
- python之pandas库学习常用函数
- 【Python】Pandas——apply函数
- Python】Pandas的apply函数使用示例
- 辗转相除法详解
- 设计模式
- 十大免费PHP编辑器-开发工具
- CSU 1979: 古怪的行列式(DFS八皇后变形)
- wust oj 1891 低价购买(最长下降子序列+方案数)【模板】
- Python-Pandas(4)自定义函数方法
- 《好未来编程题》倒置字符串
- kaggle比赛集成指南
- Android 源码中添加自定义服务并提供给上层使用
- Enable multithreading to use std::thread: Operation not permitted
- javascript中不易分清的slice,splice和split三个函数
- JAVA中s = s + 1 与s +=1的区别
- Python3 之日志设置:
- QNX操作系统优先级以及调度策略-qnx系统调度策略-任务切换方法