Skip to content
Advertisement

how to select integer columns through list comprehension in python

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?

Advertisement

Answer

Building on @nacho’s comment:

[i for i in df.columns.to_list() if isinstance(i,str) and i.endswith("_R")]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement