常用数据编辑-相加对齐
import pandas as pd import numpy as np np.random.seed(7) df1 = pd.DataFrame(np.arange(20).reshape(5, 4), columns=['A', 'B', 'C', 'D']) df2 = pd.DataFrame(np.arange(21).reshape(7, 3), columns=['A', 'B', 'C']) data = df1.add(df2) print('缺失值自动用NaN表示') print(data) data = df1.add(df2, fill_value=0) print('缺失值填充0') print(data) print('缺失值填充矩阵均值,但是形状不同引起的缺失值还是不能消除') fill1 = df1.stack().mean() data = df1.add(df2, fill_value=fill1) print(data) fill2 = df1.mean() print(fill2)