一 、
1、排序,默认从小到大
food_info.sort_values("Sodium_(mg)",inplace=True) print(food_info["Sodium_(mg)"])
inplace是不返回新的frame,也就是在原来的基础上
从大到小排列:
food_info.sort_values("Sodium_(mg)",inplace=True,ascending=False)
print(food_info["Sodium_(mg)"])
2、读取titanic_train.csv的数据,并显示默认的前5行
import pandas as pd #造pandas的别名为pd import numpy as np #造numpy的别名为np titanic_survival = pd.read_csv("titanic_train.csv") titanic_survival.head() #head()无参数,默认返回数据的前5行
3、
age_is_null = pd.isnull(age) #print(age_is_null) #null= titanic_survival["Age"][age_is_null==False] #null null= titanic_survival["Age"][age_is_null] ~ null= titanic_survival["Age"][age_is_null==True] print(null) #good_ages = titanic_survival["Age"][age_is_null == False] #[age_is_null == False]若没有缺失值,则保留。(由此滤去缺失值) #print(good_ages)
输出结果:
5 NaN 17 NaN 19 NaN 26 NaN 28 NaN 29 NaN 31 NaN 32 NaN 36 NaN 42 NaN 45 NaN 46 NaN 47 NaN 48 NaN 55 NaN 64 NaN 65 NaN 76 NaN 77 NaN 82 NaN 87 NaN 95 NaN 101 NaN 107 NaN 109 NaN 121 NaN 126 NaN 128 NaN 140 NaN 154 NaN .. 718 NaN 727 NaN 732 NaN 738 NaN 739 NaN 740 NaN 760 NaN 766 NaN 768 NaN 773 NaN 776 NaN 778 NaN 783 NaN 790 NaN 792 NaN 793 NaN 815 NaN 825 NaN 826 NaN 828 NaN 832 NaN 837 NaN 839 NaN 846 NaN 849 NaN 859 NaN 863 NaN 868 NaN 878 NaN 888 NaN Name: Age, Length: 177, dtype: float64
4、
#将船舱等级进行一个转化,1——First Class.... 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) #axis=1的时候是横着看 print(classes) 输出结果: 0 Third Class 1 First Class 2 Third Class 3 First Class 4 Third Class 5 Third Class 6 First Class 7 Third Class 8 Third Class 9 Second Class 10 Third Class 11 First Class 12 Third Class 13 Third Class 14 Third Class 15 Second Class 16 Third Class 17 Second Class 18 Third Class 19 Third Class 20 Second Class 21 Second Class 22 Third Class 23 First Class 24 Third Class 25 Third Class 26 Third Class 27 First Class 28 Third Class 29 Third Class ... 861 Second Class 862 First Class 863 Third Class 864 Second Class 865 Second Class 866 Second Class 867 First Class 868 Third Class 869 Third Class 870 Third Class 871 First Class 872 First Class 873 Third Class 874 Second Class 875 Third Class 876 Third Class 877 Third Class 878 Third Class 879 First Class 880 Second Class 881 Third Class 882 Third Class 883 Second Class 884 Third Class 885 Third Class 886 Second Class 887 First Class 888 Third Class 889 First Class 890 Third Class Length: 891, dtype: object
二 、对数据进行处理
1. 用 .isnull()来处理数据的缺失值
其实数据都有缺失值,在进行数据处理的时候首先对缺失值要有一个详细的了解。
下边将通过对列“age”列的处理来看一下缺失值的情况的。
用.isnull()可以返回缺失值的情况。若当前值缺失,返回true,否则返回false。