0

I want to create a new dataframe that will be exported to csv using selected columns from the existing dataframe.

I've tried using a for loop to add every column within range(14, var_x, 2). So all the even numbered columns after columns[14].

original code:

var_col_length=len(df.columns)

for x in range(14, var_col_length, 2):
    new_dataframe=df[df.columns[x]]

using append:

var_col_length=len(df.columns)

for x in range(14, var_col_length, 2):
    new_dataframe=new_dataframe.append(df[df.columns[x]])


TypeError: '<' not supported between instances of 'str' and 'int'

I either get only the last column, since as expected of the above code it keeps rewriting the dataframe "new_dataframe", when I tried using .append I get an error:

TypeError: '<' not supported between instances of 'str' and 'int'.

2 Answers 2

1

Use .iloc

new_df = df.iloc[:, 14:var_col_length:2]
Sign up to request clarification or add additional context in comments.

4 Comments

How could I add an if or where statement to it?
@Mitch The documentation on loc and iloc provides example to select rows and columns based on some conditions.
Ya I can't find an example using endswith() and I'm getting an error when I try.
@Mitch Scroll around for endswith and startswith on here, I'm sure you'll find something !
0
import numpy as np
df[df.columns[np.arange(14,var_col_length,2)]]

2 Comments

Thanks @Ferran how would you an "if" statement to that?
df.columns.isin() for filtering columns with conditions

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.