前言
系列文章目录
视频及资料和课件
链接:https://pan.baidu.com/s/1LCv_qyWslwB-MYw56fjbDg?pwd=1234
提取码:1234
包的引入:
import numpy as np import pandas as pd
1. DataFrame 对象创建
1.1 通过列表创建 DataFrame 对象
l = [1, 2, 3, 4, 5] df = pd.DataFrame(l) print(df) print() print(type(df))
l = [ ['zs', 12, 'm'], ['ls', 23, 'm'], ['ww', 22, 'm'] ] df1 = pd.DataFrame(l) print(df1) print() print(type(df1)) print()
l = [ {'zs', 12, 'm'}, {'ls', 23, 'm'}, {'ww', 22, 'm'} ] df1 = pd.DataFrame(l) print(df1) print() print(type(df1)) print()
由于集合是无序的,所以创建的 DataFrame 对象中元素的顺序也无序。
1.2 通过元组创建 DataFrame 对象
t = (1, 2, 3, 4, 5) df = pd.DataFrame(t) print(df) print() print(type(df))
l = ( ['zs', 12, 'm'], ['ls', 23, 'm'], ['ww', 22, 'm'] ) df1 = pd.DataFrame(l) print(df1) print() print(type(df1)) print()
l = ( {'zs', 12, 'm'}, {'ls', 23, 'm'}, {'ww', 22, 'm'} ) df1 = pd.DataFrame(l) print(df1) print() print(type(df1)) print()
1.3 通过集合创建 DataFrame 对象
集合内不能嵌套集合、列表
s = {1, 2, 3, 4, 5, 2, 2, 5, 6} df = pd.DataFrame(s) print(df) print() print(type(df))
l = { ('zs', 12, 'm'), ('ls', 23, 'm'), ('ww', 22, 'm') } df1 = pd.DataFrame( l, columns=['name', 'age', 'gender'], index=['a', 'b', 'c'], dtype='float64' ) print(df1) print() print(type(df1)) print()
1.4 通过字典创建 DataFrame 对象
d = { 'zs': 12, 'ls': 23, 'ww': 22 } # 只有一层字典必须使用 index 指定索引 # index 指定的索引为行索引 # 字典的 key 为列索引 df = pd.DataFrame(d, index=['age']) print(df) print() print(type(df))
d = { 'zs': {'age': 12, 'gender': 'm'}, 'ls': {'age': 23, 'gender': 'm'}, 'ww': {'age': 22, 'gender': 'm'} } # 多层字典不用使用 index 指定索引 # 外层字典的 key 为列索引 # 内层字典的 key 为行索引 df = pd.DataFrame(d) print(df) print() print(type(df))
d = { 'zs': [12, 'm'], 'ls': [23, 'm'], 'ww': [22, 'm'] } df1 = pd.DataFrame(d) print(df1) print() print(type(df1)) print() df2 = pd.DataFrame(d, index=['age', 'gender']) print(df2) print() print(type(df2))
1.5 通过Series 对象创建 DataFrame 对象
l = pd.Series([1,2,3]) df = pd.DataFrame(l) print(df) print() print(type(df))
l = [ pd.Series([1,2,3]), pd.Series([4,5,6]), pd.Series([7,8,9]) ] df = pd.DataFrame(l) print(df) print() print(type(df))
1.6 通过 ndarray 创建 DataFrame 对象
l = np.array([1,2,3]) df = pd.DataFrame(l) print(df) print() print(type(df))
l = [ np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9]) ] df = pd.DataFrame(l) print(df) print() print(type(df))
1.7 创建 DataFrame 对象时指定列索引
- columns:指定列索引
l = [ ['zs', 12, 'm'], ['ls', 23, 'm'], ['ww', 22, 'm'] ] df1 = pd.DataFrame(l, columns=['name', 'age', 'gender']) print(df1) print() print(type(df1)) print()