Substitution Index from Pandas DataFrame

I have a DataFrame with columns [A, B, C, D, E, F, G, H] .

The index was made with the columns [D, G, H] :

 >>> print(dgh_columns) Index(['D', 'G', 'H'], dtype='object') 

How can I get the original DataFrame without columns D, G, H ?

Is there a subset operation of indices?

Ideally, this would be:

 df[df.index - dgh_columns] 

But it does not work

+5
source share
2 answers

I think you can use Index.difference :

 df[df.columns.difference(dgh_columns)] 

Example:

 df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9], 'D':[1,3,5], 'E':[7,8,9], 'F':[1,3,5], 'G':[5,3,6], 'H':[7,4,3]}) print (df) ABCDEFGH 0 1 4 7 1 7 1 5 7 1 2 5 8 3 8 3 3 4 2 3 6 9 5 9 5 6 3 dgh_columns = pd.Index(['D', 'G', 'H']) print (df[df.columns.difference(dgh_columns)]) ABCEF 0 1 4 7 7 1 1 2 5 8 8 3 2 3 6 9 9 5 

Numpy solution with numpy.setxor1d or numpy.setdiff1d :

 dgh_columns = pd.Index(['D', 'G', 'H']) print (df[np.setxor1d(df.columns, dgh_columns)]) ABCEF 0 1 4 7 7 1 1 2 5 8 8 3 2 3 6 9 9 5 

 dgh_columns = pd.Index(['D', 'G', 'H']) print (df[np.setdiff1d(df.columns, dgh_columns)]) ABCEF 0 1 4 7 7 1 1 2 5 8 8 3 2 3 6 9 9 5 
+5
source

use drop

 df.drop(list('DGH'), axis=1) 

 df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6], 'C':[7,8,9], 'D':[1,3,5], 'E':[7,8,9], 'F':[1,3,5], 'G':[5,3,6], 'H':[7,4,3]}) df.drop(list('DGH'), 1) 

enter image description here

+2
source

Source: https://habr.com/ru/post/1259410/


All Articles