2018-11-30课程03

数据科学之3-3深入理解Series和DataFrame

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
data = {'Country': ['Belgium', 'India', 'Brazil'],'Capital':['Brussles','New Delhi', 'Brasilia'],
       'Population':[111190846, 1303171035, 207847528]}

Series

s1 = pd.Series(data['Country'])
s1
0    Belgium
1      India
2     Brazil
dtype: object
s1.values
array(['Belgium', 'India', 'Brazil'], dtype=object)
s1.index
RangeIndex(start=0, stop=3, step=1)

DataFrame

df1 = pd.DataFrame(data)
df1
































Country Capital Population
0 Belgium Brussles 111190846
1 India New Delhi 1303171035
2 Brazil Brasilia 207847528

df1.iterrows()

for row in df1.iterrows():
    print(row), print(type(row))
(0, Country         Belgium
Capital        Brussles
Population    111190846
Name: 0, dtype: object)

(1, Country            India
Capital        New Delhi
Population    1303171035
Name: 1, dtype: object)

(2, Country          Brazil
Capital        Brasilia
Population    207847528
Name: 2, dtype: object)

DataFrame的IO操作

import webbrowser
link = "http://pandas.pydata.org/pandas-docs/version/0.20/io.html"
webbrowser.open(link)
True
df1 = pd.read_clipboard()
df1










































































































Format Type Data Description Reader Writer
0 text CSV read_csv to_csv
1 text JSON read_json to_json
2 text HTML read_html to_html
3 text Local clipboard read_clipboard to_clipboard
4 binary MS Excel read_excel to_excel
5 binary HDF5 Format read_hdf to_hdf
6 binary Feather Format read_feather to_feather
7 binary Msgpack read_msgpack to_msgpack
8 binary Stata read_stata to_stata
9 binary SAS read_sas
10 binary Python Pickle Format read_pickle to_pickle
11 SQL SQL read_sql to_sql
12 SQL Google Big Query read_gbq to_gbq

# 复制一个12这个数字然后执行这个语句 会把df1中的数据写到粘贴板里
df1.to_clipboard()
df1.to_csv('df1.csv', index=False) # 去掉行索引
!ls
!more df1.csv
# 读取 csv
df2 = pd.read_csv('df1.csv')
df2










































































































Format Type Data Description Reader Writer
0 text CSV read_csv to_csv
1 text JSON read_json to_json
2 text HTML read_html to_html
3 text Local clipboard read_clipboard to_clipboard
4 binary MS Excel read_excel to_excel
5 binary HDF5 Format read_hdf to_hdf
6 binary Feather Format read_feather to_feather
7 binary Msgpack read_msgpack to_msgpack
8 binary Stata read_stata to_stata
9 binary SAS read_sas
10 binary Python Pickle Format read_pickle to_pickle
11 SQL SQL read_sql to_sql
12 SQL Google Big Query read_gbq to_gbq

df1.to_json()
'{"Format Type":{"0":"text","1":"text","2":"text","3":"text","4":"binary","5":"binary","6":"binary","7":"binary","8":"binary","9":"binary","10":"binary","11":"SQL","12":"SQL"},"Data Description":{"0":"CSV","1":"JSON","2":"HTML","3":"Local clipboard","4":"MS Excel","5":"HDF5 Format","6":"Feather Format","7":"Msgpack","8":"Stata","9":"SAS","10":"Python Pickle Format","11":"SQL","12":"Google Big Query"},"Reader":{"0":"read_csv","1":"read_json","2":"read_html","3":"read_clipboard","4":"read_excel","5":"read_hdf","6":"read_feather","7":"read_msgpack","8":"read_stata","9":"read_sas","10":"read_pickle","11":"read_sql","12":"read_gbq"},"Writer":{"0":"to_csv","1":"to_json","2":"to_html","3":"to_clipboard","4":"to_excel","5":"to_hdf","6":"to_feather","7":"to_msgpack","8":"to_stata","9":" ","10":"to_pickle","11":"to_sql","12":"to_gbq"}}'
pd.read_json(df1.to_json())










































































































Format Type Data Description Reader Writer
0 text CSV read_csv to_csv
1 text JSON read_json to_json
10 binary Python Pickle Format read_pickle to_pickle
11 SQL SQL read_sql to_sql
12 SQL Google Big Query read_gbq to_gbq
2 text HTML read_html to_html
3 text Local clipboard read_clipboard to_clipboard
4 binary MS Excel read_excel to_excel
5 binary HDF5 Format read_hdf to_hdf
6 binary Feather Format read_feather to_feather
7 binary Msgpack read_msgpack to_msgpack
8 binary Stata read_stata to_stata
9 binary SAS read_sas

df1.to_html('df.html')
df1.to_excel('df1.xlsx')

你可能感兴趣的:(2018-11-30课程03)