Python Forum
Adapting a dataframe to the some of columns - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Adapting a dataframe to the some of columns (/thread-28963.html)



Adapting a dataframe to the some of columns - flyway - Aug-11-2020

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


RE: Adapting a dataframe to the some of columns - scidam - Aug-12-2020

.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].


RE: Adapting a dataframe to the some of columns - flyway - Aug-12-2020

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