Home > Back-end >  how to select integer columns through list comprehension in python
how to select integer columns through list comprehension in python

Time:07-13

I have a dataframe with columns as mentioned

[-10800,
 -9000,
 -7200,
 -5400,
 -3600,
 -1800,
 0,
 180,
 300,
 1200,
 1800,
 2400,
 3600,
 '-10800_R',
 '-9000_R',
 '-7200_R',
 '-5400_R',
 '-3600_R',
 '-1800_R',
 '0_R',
 '180_R',
 '300_R',
 '1200_R',
 '1800_R',
 '2400_R',
 '3600_R']

I want to only select the '_R' columns

I am using list comprehension

[i for i in df.columns.to_list() if '_R' in i]

but i am getting the error of

TypeError: argument of type 'int' is not iterable

Is there another way to do it?

CodePudding user response:

Building on @nacho's comment:

[i for i in df.columns.to_list() if isinstance(i,str) and i.endswith("_R")]

CodePudding user response:

You need to add a check that i is a string:

[i for i in df.columns.to_list() if isinstance(i, str) and '_R' in i]
  • Related