3

didnt see yet how it would be because i don't want to remember the names of the columns, also maybe my columns are integer, just dropping them by their position i'm interested in, any idea? No info on the documentation

Thank you so much

Update

For example:

How to drop by index in column *

del df[0,1,2,3]  # Doesn't work
df.drop(df.columns[[0,1,2,3]], axis=1)  # Doesn't work because it has a list instead of one column, i mean dropping multiple columns not just one

my DF:

                                     help  ... success
_links       https://opendata.com/data...  ...    True
fields       https://opendata.com/data...  ...    True
Community
  • 1
  • 1
M. Mariscal
  • 1,226
  • 3
  • 17
  • 46
  • Use `df = df.drop(df.columns[[0, 1, 3]], axis=1)` – jezrael Mar 02 '20 at 11:10
  • AttributeError: 'list' object has no attribute 'drop', i tried it, and also del df[[0,1,2,3]] but nothing – M. Mariscal Mar 02 '20 at 11:14
  • Thank you for edit. I test `df = pd.DataFrame({ 'A':list('abcdef'), 'B':[4,5,4,5,5,4], 'C':[7,8,9,4,2,3], 'D':[1,3,5,7,1,0], 'E':[5,3,6,9,2,4], 'F':list('aaabbb') })` – jezrael Mar 02 '20 at 11:26
  • with `df = df.drop(df.columns[[0,1,2,3]], axis=1)` and working correctly. – jezrael Mar 02 '20 at 11:27
  • If there are lists please change data sample for lists, be free use data sample from comment above. – jezrael Mar 02 '20 at 11:30
  • https://stackoverflow.com/questions/60487625/how-to-drop-columns-without-writing-every-name-in-pandas?noredirect=1#comment107006102_60487625 – jezrael Mar 02 '20 at 11:40
  • I'm so sorry i updated the question, i was watching different dataframe (multitask problems jeje) still with the same error – M. Mariscal Mar 02 '20 at 11:45
  • Sorry, I cannot see any data in question. So how I can test your not working solution? – jezrael Mar 02 '20 at 11:46
  • tested `df = pd.DataFrame({ 0:list('abcdef'), 1:[4,5,4,5,5,4], 7:[7,8,9,4,2,3], 30:[1,3,5,7,1,0], 50:[5,3,6,9,2,4], 100:list('aaabbb') })` and working correct – jezrael Mar 02 '20 at 11:46
  • sorry column names arent numbers, i meant and updated, to deleting it by index in columns – M. Mariscal Mar 02 '20 at 11:50
  • OK, if using `print (df.columns[[0,1,2,3]])` it return first 4 columns ? – jezrael Mar 02 '20 at 11:51
  • AttributeError: 'list' object has no attribute 'columns' says, im using python 27, maybe is the problem? i said on the question – M. Mariscal Mar 02 '20 at 11:55
  • I think proble is `df` is `list`, can you check it? – jezrael Mar 02 '20 at 11:57
  • Ok i see the problem, casting to dataframe it makes it, thank you so much for your help, sorry for the missunderstood, multitask world problems... do the asnwer and i accept it ;) – M. Mariscal Mar 02 '20 at 12:00

1 Answers1

3

Problem was original solution not working, because df was list.

So first update as list and then cast to DataFrame for avoid it.

Then working correctly:

#remove columns by indexing
df1 = df1.drop(df1.columns[[0, 1, 3]], axis=1)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252