Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select columns from dataframe on condition they exist

I have a pandas DataFrame with multiple columns (columns names are numbers; 1, 2, ...) and I want to copy some of them if they do exist.

For example df1 = df[[1,2,3,4]] But it might happen that some columns do not exist in df, eg df might only have columns 1, 2, and 4 or columns 1, and 2 etc

like image 391
astudentofmaths Avatar asked Apr 21 '17 08:04

astudentofmaths


People also ask

How do I check if multiple columns exist in DataFrame Python?

In order to check if a list of multiple selected columns exist in pandas DataFrame, use set. issubset . For Example, if set(['Courses','Duration']). issubset(df.


2 Answers

Use isin with loc to filter, this will handle non-existent columns:

In [97]:
df = pd.DataFrame(columns=[1,2,4])
df.loc[:,df.columns.isin([1,2,3,4,])]

Out[97]:
Empty DataFrame
Columns: [1, 2, 4]
Index: []
like image 82
EdChum Avatar answered Oct 07 '22 22:10

EdChum


It is simpler to directly calculate the set of common columns and ask for them:

df[df.columns & [1, 2, 3, 4]]

(The & operator is the (set) intersection operator.)

like image 27
Eric O Lebigot Avatar answered Oct 07 '22 20:10

Eric O Lebigot