Dropping a number of columns in a pandas DataFrame on one line

Hartun

Greetings so I have a pandas DataFrame which looks like this:

    Product_Code  W0  W1  W2  W3  W4  W5  W6  W7  W8      ...        \
806         P815   0   0   1   0   0   2   1   0   0      ...         
807         P816   0   1   0   0   1   2   2   6   0      ...         
808         P817   1   0   0   0   1   1   2   1   1      ...         
809         P818   0   0   0   1   0   0   0   0   1      ...         
810         P819   0   1   0   0   0   0   0   0   0      ...         

     Normalized 42  Normalized 43  Normalized 44  Normalized 45  \
806           0.00           0.33           0.33           0.00   
807           0.43           0.43           0.57           0.29   
808           0.50           0.00           0.00           0.50   
809           0.00           0.00           0.00           0.50   
810           0.00           0.00           0.00           0.00   

but I don't need these columns as a matter of fact I need only W0 and W4, So I wanna remove all of them so this is what I tried:

raw_data = [ raw_data.drop( [i], 1, inplace = True )  for i in raw_data if i is not 'W0' and i is not  'W4'  ]

after half an hour I figured out that for some reason that != does not work strings and I was wondering why? so I have a stable solution:

#WORKS !!!!
# for i in raw_data:
#     if i != 'W0' and i != 'W4':
#         raw_data.drop( [i], 1, inplace = True )  

But I don't like it at all and I have commented it because it takes a lot of space and it's not beautiful, I want to make the one-line loop if expression to work, is it possible, the problem is that:

  raw_data = [ raw_data.drop( [i], 1, inplace = True )  for i in raw_data if i != 'W0' and i != 'W4'  ]

tries to convert the DataFrame to a list, how should it be done?

Clock Slave

You can use:

raw_data.drop([i for i in raw_data if i is not 'W0' and i is not  'W4'], 
               axis=1, inplace=True)

This answers the question but the condition that you state doesn't make sense. The condition you have put is if i is not 'W0' and i is not 'W4', this is going to be true always. You probably need to look at the condition again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas groupby dropping columns

From Dev

Pandas: dropping all columns with nans, 0, and NA from DataFrame

From Dev

Pandas dataframe grouping by multiple columns and dropping duplicate rows

From Dev

Pandas: dropping all columns with nans, 0, and NA from DataFrame

From Dev

Pandas dataframe grouping by multiple columns and dropping duplicate rows

From Dev

pandas dropping of words in dataframe

From Dev

Reduce number of columns in a pandas DataFrame

From Dev

Pandas - Dropping multiple empty columns

From Dev

Dropping multiple columns in pandas at once

From Dev

Sum DataFrame Columns without Dropping Columns

From Dev

Pandas: adding several columns to a dataframe in a single line

From Dev

how to split 'number' to separate columns in pandas DataFrame

From Dev

Number of levels (depth) of index and columns in a Pandas DataFrame

From Dev

pandas dataframe drop columns by number of nan

From Dev

Restructuring pandas dataframe based on number of columns

From Dev

Pandas Dataframe Transpose to varying number of columns

From Dev

Replacing Columns from one dataframe with columns from another dataframe in pandas

From Dev

Replacing Columns from one dataframe with columns from another dataframe in pandas

From Dev

Statistics of one hot encoded columns in pandas dataframe

From Dev

Define two columns with one map in Pandas DataFrame

From Dev

Pandas, DataFrame: Splitting one column into multiple columns

From Dev

Statistics of one hot encoded columns in pandas dataframe

From Dev

Pandas, DataFrame: Splitting one column into multiple columns

From Dev

Match one to many columns in Pandas dataframe

From Dev

Python Pandas Groupby Dropping DateTime Columns

From Dev

pandas: dropping columns based on value in last row

From Dev

dropping empty columns in pandas 0.23+

From Dev

Python Pandas Groupby Dropping DateTime Columns

From Dev

pandas: dropping columns based on value in last row

Related Related

  1. 1

    pandas groupby dropping columns

  2. 2

    Pandas: dropping all columns with nans, 0, and NA from DataFrame

  3. 3

    Pandas dataframe grouping by multiple columns and dropping duplicate rows

  4. 4

    Pandas: dropping all columns with nans, 0, and NA from DataFrame

  5. 5

    Pandas dataframe grouping by multiple columns and dropping duplicate rows

  6. 6

    pandas dropping of words in dataframe

  7. 7

    Reduce number of columns in a pandas DataFrame

  8. 8

    Pandas - Dropping multiple empty columns

  9. 9

    Dropping multiple columns in pandas at once

  10. 10

    Sum DataFrame Columns without Dropping Columns

  11. 11

    Pandas: adding several columns to a dataframe in a single line

  12. 12

    how to split 'number' to separate columns in pandas DataFrame

  13. 13

    Number of levels (depth) of index and columns in a Pandas DataFrame

  14. 14

    pandas dataframe drop columns by number of nan

  15. 15

    Restructuring pandas dataframe based on number of columns

  16. 16

    Pandas Dataframe Transpose to varying number of columns

  17. 17

    Replacing Columns from one dataframe with columns from another dataframe in pandas

  18. 18

    Replacing Columns from one dataframe with columns from another dataframe in pandas

  19. 19

    Statistics of one hot encoded columns in pandas dataframe

  20. 20

    Define two columns with one map in Pandas DataFrame

  21. 21

    Pandas, DataFrame: Splitting one column into multiple columns

  22. 22

    Statistics of one hot encoded columns in pandas dataframe

  23. 23

    Pandas, DataFrame: Splitting one column into multiple columns

  24. 24

    Match one to many columns in Pandas dataframe

  25. 25

    Python Pandas Groupby Dropping DateTime Columns

  26. 26

    pandas: dropping columns based on value in last row

  27. 27

    dropping empty columns in pandas 0.23+

  28. 28

    Python Pandas Groupby Dropping DateTime Columns

  29. 29

    pandas: dropping columns based on value in last row

HotTag

Archive