[Python] 纯文本查看 复制代码import pandas as pd
chengji = ([[100, 95, 100, 99], [90, 98, 99, 100], [88, 95, 98, 88],
[99, 98, 97, 87], [96.5, 90, 96, 85], [94, 94, 93, 91],
[91, 99, 92, 87], [85, 88, 85, 90], [90, 92, 99, 88],
[90, 88, 89, 81], [85, 89, 89, 82], [95, 87, 86, 88],
[90, 97, 97, 98], [80, 92, 89, 98], [80, 98, 85, 81],
[98, 88, 95, 92]])
data = pd.DataFrame(chengji, columns=['语文', '英语', '数学', '政治'])
print(data)
# data1=data[['数学','语文','英语','政治']] #排序
# data1=data1.reset_index(drop=True) #序列重建
# data1.index.names=['序号'] #序列重命名
data['name']=data.index.map(lambda x:"wang"+str(x) )
print(data)
data2=data.copy()
hebing=['hb']
hebingh=['wang1','wang2','wang3']
data.loc['hebing']=data.loc[data['name'].isin(hebingh)].sum()
data1=data[~data['name'].isin(hebingh)]
print('方法一正确',data1)
data.drop(data['name'].isin(hebingh),inplace=True)
print(data)
data3=data2.copy()
print(data.loc[data['name'].isin(hebingh)].sum(axis=0))
data3=data3.append(data.loc[data['name'].isin(hebingh)].sum(axis=0),ignore_index=True)
data3=data3[~data3['name'].isin(hebingh)]
data3.loc[data3.index[-1],'name']=hebing[0]#取得最后一个index并改名
print('方法三试成功',data3)
heji=['hb', ['wang1','wang2','wang3']]
print('方法2出错',data2)
# data2.loc[heji[0]] = (data2[data2['p'] == heji[1][0]].values + data2[data2['p'] == heji[1][1]].values)[0]
# data2.drop(data2['p'].isin(heji[1]), inplace=True)
data2.loc[heji[0]] = (data2[data2['name'] == heji[1][0]].values + data2[data2['name'] == heji[1][1]].values++ data2[data2['name'] == heji[1][2]].values)[0]
# data2.drop(data2['p'].isin(heji[1]), inplace=True)data2.append(data2.loc[data2['p'].isin(heji[1])].sum(axis=0),ignore_index=True)
print(data2,heji[1])
data2.drop(data2['name'].isin(heji[1]),inplace=True) #此语句出错
print(data2)