I have a pandas dataframe which has 50 columns and I want to choose 30 columns. Let's say we have the following dataframe:
df = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",............, "z"]
I would like to choose "a" and "c" and then all the columns from "h" until "z"
Please advise on how to subset this dataframe either by name or by index.
I would like the resulting dataframe to look like the following:
df = ["a", "c", "h", "i", "j", "k", "l",............, "z"]
You can use numpy.r_
for concanecate positions of columns, then use iloc
for selecting:
print (df.iloc[:, np.r_[0,2,7: len(df.columns)]])
Sample:
df = pd.DataFrame({'a':[1,2,3],
'b':[4,5,6],
'c':[7,8,9],
'd':[1,3,5],
'e':[5,3,6],
'f':[7,8,9],
'g':[1,3,5],
'h':[5,3,6],
'i':[7,8,9],
'j':[1,3,5],
'k':[5,3,6],
'l':[7,4,3],
'm':[7,4,3]})
print (df)
a b c d e f g h i j k l m
0 1 4 7 1 5 7 1 5 7 1 5 7 7
1 2 5 8 3 3 8 3 3 8 3 3 4 4
2 3 6 9 5 6 9 5 6 9 5 6 3 3
print (np.r_[0,2,7: len(df.columns)])
[ 0 2 7 8 9 10 11 12]
print (df.iloc[:, np.r_[0,2,7: len(df.columns)]])
a c h i j k l m
0 1 7 5 7 1 5 7 7
1 2 8 3 8 3 3 4 4
2 3 9 6 9 5 6 3 3
Another solution is use exclude list
with difference
:
exclude = ['b','d','e','f','g']
cols = df.columns.difference(exclude)
print (cols)
Index(['a', 'c', 'h', 'i', 'j', 'k', 'l', 'm'], dtype='object')
print (df[cols])
a c h i j k l m
0 1 7 5 7 1 5 7 7
1 2 8 3 8 3 3 4 4
2 3 9 6 9 5 6 3 3
Or drop
columns:
exclude = ['b','d','e','f','g']
print (df.drop(exclude, axis=1))
a c h i j k l m
0 1 7 5 7 1 5 7 7
1 2 8 3 8 3 3 4 4
2 3 9 6 9 5 6 3 3