1.加载库
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
2.通过browser库建立DataFrame
import webbrowser
link = 'http://pandas.pydata.org/pandas-docs/version/0.20/io.html'
webbrowser.open(link)
#进入网页,右键复制表格
df = pd.read_clipboard()
df
输出如下的DataFrame
3.查看DataFrame的相关量
type(df) #查看
>>>pandas.core.frame.DataFrame
df.columns
>>>Index(['Format Type', 'Data Description', 'Reader', 'Writer'], dtype='object')
df.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
Name: Reader, dtype: object
df['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
Name: Data Description, dtype: object
type(df['Data Description']) #只取一列的数据类型
>>>pandas.core.series.Series
4.只取原DataFrame的一部分创建新的DataFrame
df_new = DataFrame(df, columns=['Data Description', 'Reader'])
df_new
df_new = DataFrame(df, columns=['Data Description', 'Reader',"2018"])
df_new #增加一列原先DF不存在的列
#给新增加的列赋值
df_new['2018'] = range(0,13) ##等同于df_new['2018'] = np.arange(0,13)
df_new
df_new['2018'] = pd.Series([100, 200], index=[1,2]) #某几个值进行赋值
df_new