
baby_names['Gender'].value_counts()
# you don't want to sum the Year column, so you delete it
del baby_names["Year"]
# group the data
names = baby_names.groupby("Name").sum()
# print the first 5 observations
names.head()
# print the size of the dataset
print(names.shape)
# sort it from the biggest value to the smallest one
names.sort_values("Count", ascending = 0).head()

len(names[names.Count == names.Count.min()])
# .std() 返回每列的标准差
names.Count.std()
names.describe()

数据已被修改为包含一些缺失值,由NaN标识。 使用pandas将使得这个处理更容易。
不要使用for 循环或其他循环构造一提高效率。
# parse_dates gets 0, 1, 2 columns and parses them as the index
data_url = 'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/06_Stats/Wind_Stats/wind.data'
data = pd.read_csv(data_url, sep = "\s+", parse_dates = [[0,1,2]])
data.head()

# The problem is that the dates are 2061 and so on...
# function that uses datetime
def fix_century(x):
year = x.year - 100 if x.year > 1989 else x.year
return datetime.date(year, x.month, x.day)
# apply the function fix_century on the column and replace the values to the right ones
data['Yr_Mo_Dy'] = data['Yr_Mo_Dy'].apply(fix_century)
# data.info()
data.head()

# transform Yr_Mo_Dy it to date type datetime64
data["Yr_Mo_Dy"] = pd.to_datetime(data["Yr_Mo_Dy"])
# set 'Yr_Mo_Dy' as the index
data = data.set_index('Yr_Mo_Dy')
data.head()
# data.info()
#number of columns minus the number of missing values for each location
data.shape[0] - data.isnull().sum()
#or
data.notnull().sum()
data.describe(percentiles=[])
data.loc[data.index.month == 1].mean()
# to_period('')描述的是该日期处于那个时期
# 例如to_period('M') 将日期以月为单位即 xxxx-xx 某天处于某个月分
data.groupby(data.index.to_period('A')).mean()

data.groupby(data.index.to_period('M')).mean()

data.groupby(data.index.to_period('W')).mean()

# resample data to 'W' week and use the functions
weekly = data.resample('W').agg(['min','max','mean','std'])
# slice it for the first 52 weeks and locations
weekly.loc[weekly.index[1:53], "RPT":"MAL"] .head(10)
