pandas - 从 Pandas dataframe中取消或者删除名为'index'的列

我正在尝试从三个父级(或源级)dataframe (每个均从csv文件创建)创建一个数据框,但是将生成的数据框写入文件或在屏幕上打印时,出现了"index"列,如何取消/删除它们?

三个'父级'dataframes :


df1 ...
 fname lname employer score1 score2 score3
0 Alice Adams IMB -1.0 2.5 -0.2
1 Alice Brown MFS 2.2 -7.9 3.7
2 Alice Curt OCR 2.6 -1.2 -0.7

df2 ...
 fname lname employer score1 score2 score3
0 Alice Adams IMB 3.0 0.1 -2.9
1 Alice Brown MFS -2.1 2.6 -1.0
2 Alice Curt OCR 3.1 1.9 -0.1

df3 ...
 fname lname employer score1 score2 score3
0 Alice Adams IMB -1.0 -2.1 0.1
1 Alice Brown MFS 3.2 -0.9 5.1
2 Alice Curt OCR -1.1 -1.2 -1.9

经过一系列的操作,我得到了这样的结果:


 fname lname index employer score1 index employer score3 index employer score1 index employer score3 index employer score1 index employer score3
0 Alice Adams 0 IMB -1.0 2 OCR -0.7 1 MFS -2.1 0 IMB -2.9 2 OCR -1.1 2 OCR -1.9
1 Alice Brown 1 MFS 2.2 0 IMB -0.2 0 IMB 3.0 1 MFS -1.0 0 IMB -1.0 0 IMB 0.1
2 Alice Curt 2 OCR 2.6 1 MFS 3.7 2 OCR 3.1 2 OCR -0.1 1 MFS 3.2 1 MFS 5.1



源代码.csv文件和.py脚本:


A.csv ... 
fname,lname,employer,score1,score2,score3
Alice,Adams,IMB,-1.0,2.5,-0.2
Alice,Brown,MFS,2.2,-7.9,3.7
Alice,Curt,OCR,2.6,-1.2,-0.7

B.csv ...
fname,lname,employer,score1,score2,score3
Alice,Adams,IMB,3.0,0.1,-2.9
Alice,Brown,MFS,-2.1,2.6,-1.0
Alice,Curt,OCR,3.1,1.9,-0.1

C.csv ...
fname,lname,employer,score1,score2,score3
Alice,Adams,IMB,-1.0,-2.1,0.1
Alice,Brown,MFS,3.2,-0.9,5.1
Alice,Curt,OCR,-1.1,-1.2,-1.9

.py脚本


# -*- coding: utf-8 -*-

import fnmatch
import os
import matplotlib.pyplot as plt
import pandas as pd

pd.set_option('display.max_columns', None)

Datasets = ['A', 'B', 'C']
bigDF = pd.DataFrame()

for fname in Datasets:
 if fname == 'A':
 csvdf = pd.read_csv(fname+'.csv')
 csvdfBUa = csvdf[['fname', 'lname']]
 csvdfBUb = csvdf[['employer', 'score1']]
 csvdfBUb = csvdfBUb.sort(['score1'], ascending=[1])
 csvdfBUb = csvdfBUb.reset_index()
 csvdfBUc = csvdf[['employer', 'score3']]
 csvdfBUc = csvdfBUc.sort(['score3'], ascending=[1])
 csvdfBUc = csvdfBUc.reset_index()
 csvdfBU = pd.concat([csvdfBUa, csvdfBUb, csvdfBUc], axis=1, ignore_index=False)
 print csvdf
 if len(bigDF.index) < 1:
 bigDF = csvdfBU
 else:
 bigDF = pd.concat([bigDF, csvdfBU], axis=1, ignore_index=False)
 elif fname == 'B':
 csvdf = pd.read_csv(fname+'.csv')
 csvdfAFb = csvdf[['employer', 'score1']]
 csvdfAFb = csvdfAFb.sort(['score1'], ascending=[1])
 csvdfAFb = csvdfAFb.reset_index()
 csvdfAFc = csvdf[['employer', 'score3']]
 csvdfAFc = csvdfAFc.sort(['score3'], ascending=[1])
 csvdfAFc = csvdfAFc.reset_index()
 csvdfAF = pd.concat([csvdfAFb, csvdfAFc], axis=1, ignore_index=False)
 print csvdf
 if len(bigDF.index) < 1:
 bigDF = csvdfAF
 else:
 bigDF = pd.concat([bigDF, csvdfAF], axis=1, ignore_index=False)
 elif fname == 'C':
 csvdf = pd.read_csv(fname+'.csv')
 csvdfGAb = csvdf[['employer', 'score1']]
 csvdfGAb = csvdfGAb.sort(['score1'], ascending=[1])
 csvdfGAb = csvdfGAb.reset_index()
 csvdfGAc = csvdf[['employer', 'score3']]
 csvdfGAc = csvdfGAc.sort(['score3'], ascending=[1])
 csvdfGAc = csvdfGAc.reset_index()
 csvdfGA = pd.concat([csvdfGAb, csvdfGAc], axis=1, ignore_index=False)
 print csvdf
 if len(bigDF.index) < 1:
 bigDF = csvdfGA
 else:
 bigDF = pd.concat([bigDF, csvdfGA], axis=1, ignore_index=False)

print bigDF

时间:

你可以使用以下方法删除"index"列:

 
del df['index']

 


import numpy as np
import pandas as pd
df[np.negative(pd.Series(df.columns).str.contains('index'))]

...