import pandas as pd import numpy as np stu_names = [“hg”,“lgx”,“jsj”,“cj”] courses = [‘yw’, ‘math’, ‘eng’, ‘Python’, ‘pe’] data = np.array([[87., 74., 98., 84., np.nan],[79., 69., 61., 99., np.nan],[84., 84., 94., 66., np.nan],[90., 60., 72., 90., np.nan]]) d=pd.DataFrame(data,index=stu_names,columns=courses) d d.to_csv(“demo.csv”)
d_backup=d d_backup
ele_names = [“hg”,“lgx”,“jsj”,“cj”] ele_courses = [‘stas’, ‘jp’] ele_data = np.array([[85., np.nan],[np.nan, 69.],[np.nan, 95.],[80., np.nan]]) d2=pd.DataFrame(ele_data,index=ele_names,columns=ele_courses) d2
d2.to_csv(“demo1.csv”) pd.read_csv?? data1=pd.read_csv(“demo.csv”) data2=pd.read_csv(“demo1.csv”)
data1 data2
del d_backup[“pe”] d_backup
d3=d2.T d4=d_backup.T d5=d4.append(d3) d6=d5.T d6 d7=d6.sum(axis=1) d7 d6[“sum”]=d7 d6
d6.to_csv(“demo2.csv”)
Excel结果运行如下: 代码如下