0

My dataset looks like this(first row is header)

0    1    2    3    4    5
1    3    4    6    2    3
3    8    9    3    2    4
2    2    3    2    1    2

I want to select a range of columns of the dataset based on the column [5], e.g:

1    3    4
3    8    9    3
2    2

I have tried the following, but it did not work:

df.iloc[:,0:df['5'].values]
1
  • What kind of output you want? If it s a dataframe, do you want to replace missing values with nans? Commented Dec 7, 2017 at 3:04

2 Answers 2

2

Let's try:

df.apply(lambda x: x[:x.iloc[5]], 1)

Output:

     0    1    2    3
0  1.0  3.0  4.0  NaN
1  3.0  8.0  9.0  3.0
2  2.0  2.0  NaN  NaN
Sign up to request clarification or add additional context in comments.

2 Comments

Nice one :-) I like the usage of apply
apply is neater :-)
2

Recreate your dataframe

df=pd.DataFrame([x[:x[5]] for x in df.values]).fillna(0)
df
Out[184]: 
   0  1    2    3
0  1  3  4.0  0.0
1  3  8  9.0  3.0
2  2  2  0.0  0.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.