1. 创建Series对象
1、 列表生成Series
import pandas as pdimport numpy as npa = pd.Series([3, 2, 4, 1, 5])print(a)print(a.values)print(a.index)0 31 22 43 14 5dtype: int64[3 2 4 1 5]RangeIndex(start=0, stop=5, step=1)
2、 新建Series时指定索引
b = pd.Series([3, 2, 4, 1, 5], index=('b', 'a', 'd', 'w', 'y'))print(b)print(b.values)print(b.index)b 3a 2d 4w 1y 5dtype: int64[3 2 4 1 5]Index(['b', 'a', 'd', 'w', 'y'], dtype='object')
3、 从Numpy数组生成Series
index默认值是整数序列:Series通用的Numpy数组,Numpy是隐式索引值
a = np.arange(3)b = pd.Series(a)print(b)0 01 12 2dtype: int32
4、 从标量生成Series
标量生成Series,index默认值是整数序列
print(pd.Series(4, index=[100, 200, 300]))100 4200 4300 4dtype: int64
5、 字典生成Series
index是字典键;Series是特殊的字典,字典键是Series的索引
population_dict = {'a': 12, 'b': 25, 'c': 56, 'd': 67, 'e': 42}print("population_dict:\n", population_dict)population = pd.Series(population_dict)print("population:\n", population)population_dict:{'a': 12, 'b': 25, 'c': 56, 'd': 67, 'e': 42}population:a 12b 25c 56d 67e 42dtype: int64
6、 通过指定索引值生成Series
print(pd.Series({2: 'a', 1: 'b', 3: 'c'}, index=[3, 1, 2]))print(pd.Series(population, index=['a', 'd']))3 c1 b2 adtype: objecta 12d 67dtype: int64
7、 使用DataFrame的一列映射Series数据
print(cities['area'])a 12b 25c 56d 67e 42Name: area, dtype: int64
2. 创建DateFrame对象
1、 通过单个Series对象创建
import numpy as npimport pandas as pd# 通过单个Series对象创建a = np.arange(3)b = pd.Series(a)print(pd.DataFrame(b))00 01 12 2
2、典键值总是对应列名
data = [{'a': 12, 'b': 25}, {'a': 56, 'b': 67}]print(pd.DataFrame(data))a b0 12 251 56 67
3、 有规律的创建
data = [{'a': i, 'b': 2 * i} for i in range(3)]print(pd.DataFrame(data))a b0 0 01 1 22 2 4
4、 创建时有缺失值处理
创建时数据有缺失值时,缺失值一般用NaN(Not a Number)表示
data = [{'a': 12, 'b': 25}, {'a': 56, 'c': 67}]print(pd.DataFrame(data))a b c0 12 25.0 NaN1 56 NaN 67.0
5、 使用Series对象字典创建
population_dict = {'a': 12, 'b': 25, 'c': 56, 'd': 67, 'e': 42}print("population_dict:\n", population_dict)population = pd.Series(population_dict)print(pd.DataFrame(population))population_dict:{'a': 12, 'b': 25, 'c': 56, 'd': 67, 'e': 42}0a 12b 25c 56d 67e 42
6、 使用不同类型数据创建
使用字典作为其中一列数据,Series对象字典作为另一列数据进行创建
print(population_dict)print(population)cities = pd.DataFrame({'area': population_dict, 'bb': population})print(cities){'a': 12, 'b': 25, 'c': 56, 'd': 67, 'e': 42}a 12b 25c 56d 67e 42dtype: int64area bba 12 12b 25 25c 56 56d 67 67e 42 42
7、 使用二维数组创建
print('通过二维数组创建')aa = pd.DataFrame(np.random.rand(3, 2), columns=['foo', 'bar'], index=['a', 'b', 'c'])print(aa)foo bara 0.888702 0.962784b 0.923443 0.935304c 0.255582 0.105020
8、列名重命名
# 读入文件的时候 重命名df = pd.read_csv('xxx.csv', names=new_columns, header=0)# 部分列重命名df.rename(columns={'a':'A'})# 没有指定inplace=True, df本⾝的列名并没有改变。df.rename(columns={'a':'A'}, inplace=True)# 全部列重命名# df.columns = new_columns, new_coumns# 可以是列表或元组, 但新旧列名的长度必须⼀致,否者会不匹配报错。# 这种改变⽅式是直接改变了原始数据。df.columns = ['a1', 'b1', 'c1', 'd1']# str批量修改列名# 将'a1', 'b1'...修改为'a2', 'b2'...df.columns = df.columns.str.replace('1','2')
9、字典生成DataFrame
字典的键作为列名
import pandas as pddict_data={'Name':['Bambang'],'Gender':['Male'],'Age':[25]}df = pd.DataFrame.from_dict(dict_data)df
字典的键作为index
dict_data={'Name':'Bambang','Gender':'Male','Age':25}df = pd.Series(dict_data)df = pd.DataFrame(df, columns=['index'])df

参考资料:https://wenku.baidu.com/view/d914e2aaa3116c175f0e7cd184254b35eefd1aa4.html
