# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/marongkang/MLeveryday/blob/main/MLEveryday6.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="pKZ8WHy5-QZs"
# **ML** **day6** 
# >今天的目标是 Pandas
#
#
#
#

# + colab={"base_uri": "https://localhost:8080/"} id="NvP8gwy3pSYQ" outputId="a4c93781-223f-4150-e4c1-b382574cd5a1"
import urllib.request #依旧使用Titanic.csv
url="https://raw.githubusercontent.com/marongkang/datasets/main/titanic.csv"
response=urllib.request.urlopen(url)
page=response.read()

f=open('titanic.csv','wb')
f.write(page)

# !ls -1

#从网络获取Titanic数据集

# + colab={"base_uri": "https://localhost:8080/", "height": 206} id="FfJ_ilheplKJ" outputId="d90d848f-4026-4861-d086-4017d58fb3ac"
#pandas
import pandas as pd
'''
def read_csv(filepath_or_buffer: FilePathOrBuffer, sep=',', delimiter=None, 
header='infer', names=None, index_col=None, usecols=None, squeeze=False, 
prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, 
true_values=None, false_values=None, skipinitialspace=False, skiprows=None, 
skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, 
verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, 
keep_date_col=False, date_parser=None, dayfirst=False, cache_dates=True, iterator=False, 
chunksize=None, compression='infer', thousands=None, decimal: str='.', lineterminator=None, 
quotechar='"', quoting=csv.QUOTE_MINIMAL, doublequote=True, escapechar=None, comment=None, 
encoding=None, dialect=None, error_bad_lines=True, warn_bad_lines=True, delim_whitespace=False, 
low_memory=_c_parser_defaults['low_memory'], memory_map=False, float_precision=None)
可以传一大堆参数
'''
dataframe=pd.read_csv('titanic.csv',header=0) #header=0 指第0行为表头
dataframe.head() #获取前5行，可修改
#dataframe.head(n=10) #获取前十行

#至此数据集获取成功，下面进行数据分析

# + [markdown] id="wq5p1pBot4Xa"
# 1.  pclass: class of travel  
# 2.  name: full name of the passenger  
# 3.  sex: gender  
# 4.  age: numerical age  
# 5.  sibsp: # of siblings/spouse aboard  
# 6.  parch: number of parents/child aboard  
# 7.  ticket: ticket number  
# 8.  fare: cost of the ticket  
# 9.  cabin: location of room  
# 10.  emarked: port that the passenger embarked at (C - Cherbourg, S - Southampton,   Q= Queenstown)  
# 11.  survived: survial metric (0 - died, 1 - survived)
#
#
#

# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="Ls-XedF9rITB" outputId="8934808f-5908-4d67-f934-eb0093f823e6"
#描述性统计
dataframe.describe()
#inclue='all' 会对所有数据进行描述，否则只描述数值列
#dataframe.describe(include='all')

# + [markdown] id="4Lk41j36v7p4"
# *   count：数量统计，此列共有多少有效值
# *   unipue：不同的值有多少个
# *   mean：均值
# *   std：标准差
# *   min：最小值
# *   25%：四分之一分位数
# *   50%：二分之一分位数
# *   75%：四分之三分位数
# *   max：最大值
#

# + colab={"base_uri": "https://localhost:8080/"} id="ESSI5lCLteTz" outputId="33ed9eba-2d8a-4b6c-fe71-93f59632e421"
#通过表头获取数据
print(dataframe['Age'][0:5])
print(dataframe['Ticket'][0:5])

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="O4wUBy3_xTwW" outputId="de0cdcf0-b482-42ad-d972-8ea891166fca"
#直方图
dataframe['Age'].hist()

# + id="7rMkrC81xqfg" colab={"base_uri": "https://localhost:8080/"} outputId="914a1280-fc0b-4872-d5e2-d17bd7fc6a6e"
#唯一值
dataframe['Embarked'].unique()

# + colab={"base_uri": "https://localhost:8080/"} id="kO_ztT-Iz32B" outputId="0b3befe6-1a7c-441e-fa3d-7bb131bc4c06"
#选取数据
dataframe['Name'].head()

# + colab={"base_uri": "https://localhost:8080/", "height": 613} id="KrkNesre0L4Z" outputId="1375dfca-c55d-4226-f1ae-eab25ff7e580"
#筛选,Age==35
dataframe[dataframe['Age']==35.0]

# + colab={"base_uri": "https://localhost:8080/", "height": 363} id="0L-KZMlx0j25" outputId="6802bfbf-7846-4485-d351-98db0a62480c"
#排序
'''
def sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort',
 na_position='last', ignore_index=False, key: ValueKeyFunc=None)
'''
dataframe.sort_values('Age',ascending=False).head(n=10) #ascending=True 升序，False降序

# + colab={"base_uri": "https://localhost:8080/", "height": 235} id="iF4hpRJ21bQY" outputId="83f3aa1f-fead-4427-f51d-83621ceb0752"
#聚合与分组
group=dataframe.groupby('Survived')
print(type(group))
#group的shape为(2, 2)，不是很理解
glist=list(group)
import numpy as np
print(np.shape(glist))
group.mean()


StarcoderdataJupyterScriptsDedupFiltered