Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - drop last column of DataFrame

I have a DataFrame and I would like to drop the last column of it. Until now I had just been dropping what I believed to be the final column with

if len(fish_frame.columns) == 4: del fish_frame[3].

Right before this command, however, I drop all columns of NaNs. So that removes column [3] because it is filled with NaNs, so it fails.

I would like to say just drop the final column of the entire DataFrame. I feel like that would work perfectly.

I tried fish_frame([:-1], axis=1) but that's invalid syntax.

Any help would be appreciated thanks.

The DataFrame:

fish_frame after dropna:

                              0        1      2           4
0                         #0721      NaN    NaN         NaN
1                       GBE COD      746  $2.00   $1,492.00
2                       GBW COD   13,894  $0.50   $6,947.00
3                       GOM COD       60  $2.00     $120.00
4            GB WINTER FLOUNDER   94,158  $0.25  $23,539.50
5           GOM WINTER FLOUNDER    3,030  $0.50   $1,515.00
6                   GBE HADDOCK   18,479  $0.02     $369.58
7                   GOM HADDOCK        0  $0.02       $0.00
8                   GBW HADDOCK  110,470  $0.02   $2,209.40
9                          HAKE      259  $1.30     $336.70
10                       PLAICE    3,738  $0.40   $1,495.20
11                      POLLOCK    3,265  $0.02      $65.30
12               WITCH FLOUNDER    1,134  $1.30   $1,474.20
13                       SNE YT    1,458  $0.65     $947.70
14                        GB YT    4,499  $0.70   $3,149.30
15                      REDFISH      841  $0.02      $16.82
16  54 DAS @ $8.00/DAY = 432.00      NaN    NaN        None
like image 602
theprowler Avatar asked Jul 19 '17 17:07

theprowler


People also ask

How do I drop the last column of a pandas DataFrame?

Use drop() to remove last column of pandas dataframe. Use del keyword to drop last column of pandas dataframe. Use pop() to drop last column of pandas dataframe.

How do I remove the last two columns in python?

You can also use DataFrame. drop() method to delete the last n columns. Use axis=1 to specify the columns and inplace=True to apply the change on the existing DataFrame.

How do I drop range columns in pandas?

Pandas Drop Multiple Columns By Index You can use df. columns[[index1, index2, indexn]] to identify the list of column names in that index position and pass that list to the drop method. Note that an index is 0 based. Use 0 to delete the first column and 1 to delete the second column and so on.

How do I select the last 5 columns in pandas?

Use iloc[] to select last N columns of pandas dataframe. Use [] to select last N columns of pandas dataframe. Use tail() to select last N columns of pandas dataframe.


3 Answers

Use iloc and list indexing

fish_frame = fish_frame.iloc[:, :-1]

                              0        1      2
0                         #0721      NaN    NaN
1                       GBE COD      746  $2.00
2                       GBW COD   13,894  $0.50
3                       GOM COD       60  $2.00
4            GB WINTER FLOUNDER   94,158  $0.25
5           GOM WINTER FLOUNDER    3,030  $0.50
6                   GBE HADDOCK   18,479  $0.02
7                   GOM HADDOCK        0  $0.02
8                   GBW HADDOCK  110,470  $0.02
9                          HAKE      259  $1.30
10                       PLAICE    3,738  $0.40
11                      POLLOCK    3,265  $0.02
12               WITCH FLOUNDER    1,134  $1.30
13                       SNE YT    1,458  $0.65
14                        GB YT    4,499  $0.70
15                      REDFISH      841  $0.02
16  54 DAS @ $8.00/DAY = 432.00      NaN    NaN
like image 55
piRSquared Avatar answered Oct 16 '22 09:10

piRSquared


If you want to drop the last column

df = df.iloc[:,:-1]

if particular column needs to be dropped with index

df = df.drop(df.columns[column_index],axis=1)

with column name

df = df.drop(['column_name'],axis =1)
like image 35
Wickkiey Avatar answered Oct 16 '22 09:10

Wickkiey


Use drop with the columns index:

fish_frame = fish_frame.drop(fish_frame.columns[-1],axis=1)

Output:

                              0        1      2
0                         #0721      NaN    NaN
1                       GBE COD      746  $2.00
2                       GBW COD   13,894  $0.50
3                       GOM COD       60  $2.00
4            GB WINTER FLOUNDER   94,158  $0.25
5           GOM WINTER FLOUNDER    3,030  $0.50
6                   GBE HADDOCK   18,479  $0.02
7                   GOM HADDOCK        0  $0.02
8                   GBW HADDOCK  110,470  $0.02
9                          HAKE      259  $1.30
10                       PLAICE    3,738  $0.40
11                      POLLOCK    3,265  $0.02
12               WITCH FLOUNDER    1,134  $1.30
13                       SNE YT    1,458  $0.65
14                        GB YT    4,499  $0.70
15                      REDFISH      841  $0.02
16  54 DAS @ $8.00/DAY = 432.00      NaN    NaN
like image 8
Scott Boston Avatar answered Oct 16 '22 11:10

Scott Boston