Python Forum
Adapting a dataframe to the some of columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adapting a dataframe to the some of columns
#1
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
Reply
#2
.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].
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert several columns to int in dataframe Krayna 2 836 May-21-2021, 08:55 AM
Last Post: Krayna
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 1,461 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Difference of two columns in Pandas dataframe zinho 2 1,776 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 1,134 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 1,122 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,080 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  How to highlight dataframe columns SriRajesh 1 1,102 Feb-28-2020, 11:02 PM
Last Post: Marbelous
  Does the order of columns in the DataFrame matter? new_to_python 11 2,742 Feb-17-2020, 12:32 AM
Last Post: scidam
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 2,908 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  How modify the DataFrame columns SriRajesh 2 1,292 Sep-12-2019, 03:14 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020