Python Forum

Full Version: Adapting a dataframe to the some of columns
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello to all,

I need to automate my Var X = df.iloc[; , [i[0]], [i[1]],...
Depending on len of i[n]
Something like;

i = [5, 16, 20, ... ]

if len(i) = 1
then X = df.iloc[: , [ i[0] ]]

if len(i) = 2
then X = df.iloc[: , [ i[0], i[1] ]]

and so on....
So need to manage the number of elements in X in order to use only some of my DF columns for the features in use on a logistic regression.
Thank you.

Filipe Santos
.iloc selector should work directly, e.g.
X = df.iloc[:, inds], where inds is expected to be a list of column numbers, e.g. inds = [1, 3, 4].
Hi,

Thanks for your reply.
Problem is, I need the "inds" to be adapted to the len of my var i
I want to manage i (i = [58, 41, 5, 0, 15,...]
Then need the "iloc" to re-adjust column indexing to the len[i]
Not sure if made myself clear.
Thank you.

FS