1

I have dataframe of 130 columns, i wants to make df of each 10th column start from 0th then 3rd column and then wants to add 10

for ex:

df.columns = [0,3,13,23,33,43,53,63,73,83,93,103,113,123] 
Rakesh
  • 81,458
  • 17
  • 76
  • 113

1 Answers1

1

This is one approach using slicing

Ex:

columns = list(range(0, 131))     #sample column --> df.columns.tolist() 
result = [columns[0]] + columns[3::10]
print(result)

Output:

[0, 3, 13, 23, 33, 43, 53, 63, 73, 83, 93, 103, 113, 123]

To create new DF

new_df = df[result].copy()
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Thanks for your support, can u suggest me what should be next step to make dataframe. i am trying new_df = pd.DataFrame(df, columns = result) but its showing NaN values –  Aug 27 '19 at 12:51
  • https://stackoverflow.com/questions/34682828/extracting-specific-selected-columns-to-new-dataframe-as-a-copy – Rakesh Aug 27 '19 at 12:52
  • `new_df = df[result].copy()` – Rakesh Aug 27 '19 at 12:53
  • again its giving error,may be it copy by col lable name not by col index number –  Aug 27 '19 at 13:15
  • I used `list(range(0, 131))` as an example you should be using `df.columns.tolist() ` – Rakesh Aug 27 '19 at 13:17
  • 1
    yess...i got expected answer...thanku so much for your efforts –  Aug 27 '19 at 13:37