一、Pandas对缺失值的处理
1.1 忽略空行
skiprows
1.2 删掉全是空值的列或者行
.dropna(axis="columns" / "index",how ='any'/'all',inplace=True/False)
1.3 对缺失值进行填充
.loc[:, '列名'] = ['列名'].fillna(method="ffill")
二、数据结构
2.1 创建series
2.1.1 列表创建series
变量名 = pd.Series([xx,xx,xx])
2.1.2 创建具有标签索引的Series
变量名 = pd.Series([x,x,x,x],index=['x','xx','xxx','xxxx'])
输出标签
print(变量名['index'])
2.1.3 使用字典创建Series
变量名1 = {'x':12,'xx':123,'xxx':1234}
变量名2 = pd.Series(变量名1)
2.2 Dataframe
创建dataframe最常用的方式就是读取文件
2.2.1 根据字典序列创建dataframe
变量名1 = {'属性1':['x','xx','xxx'],'属性2':['x','xx','xxx']}
变量名2 = pd.Dataframe(变量名1)
三、索引index
3.1 使用index查询数据
变量名.set_index("索引名",inplace = True,drop=False) //drop = False 让索引列依旧保留在原本的列中
四、Merge合并
4.1 inner join
how = 'inner' 会显示两者共有的属性
4.2 outer join
how = 'outer' 会显示两者全部的属性
4.3 right join
how = 'right' 右边的属性会全保留 左边会留下与右边匹配的属性
4.4 left join
how = 'left' 左边的属性会全保留 右边会留下与左边匹配的属性
4.5 出现非Key的字段重名
key不变
使用suffixes=('字段名1','字段名2')来自己指定重复字段的名称
引号里的内容会被添加后原来的column后面
# 五、Concat合并
axis = 0,为竖向合并
axis = 1,为横向合并
ignore_index = True,行从0开始算,而不是每个数据集单独显示
join ='inner'/'outer'
inner会显示两者共有的属性,outer是都会显示,默认为outer
// df1.append(df2) 会直接将df2插入到df1的后面
六、Join合并
df1.join(df2,on =' key',how='outer',lsuffix='',rsuffix='')
how为连接方式 与上述一样
lsuffix与rsuffix在连接重复的key值时,必须重新设置column名,不然会报错



