Pandas - Drop Last Column of Dataframe

How to delete the last column of data of a pandas dataframe

You can specify which columns to import using usecols parameter for read_csv

So either create a list of column names or integer values:

cols_to_use = ['col1', 'col2'] # or [0,1,2,3]
df = pd.read_csv('mycsv.csv', usecols= cols_to_use)

or drop the column after importing, I prefer the former method (why import data you are not interested in?).

df = df.drop(labels='column_to_delete', axis=1) # axis 1 drops columns, 0 will drop rows that match index value in labels

Note also you misunderstand what tail does, it returns the last n rows (default is 5) of a dataframe.

Additional

If the columns are varying length then you can just the header to get the columns and then read the csv again properly and drop the last column:

def df_from_csv(path):
df = read_csv(path, nrows=1) # read just first line for columns
columns = df.columns.tolist() # get the columns
cols_to_use = columns[:len(columns)-1] # drop the last one
df = read_csv(path, usecols=cols_to_use)
return df

Pandas - drop last column of DataFrame

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

How Can I drop a column if the last row is nan

You can also do something like this

df.loc[:, ~df.iloc[-1].isna()]
    A   C
0 NaN x
1 1 3
2 x z
3 4 6

Move a dataframe column to last column

You can pop and insert again:

df['X'] = df.pop('X')

example:

df = pd.DataFrame([list('axbc')], columns=['A', 'X', 'B', 'C'])
print(df)

A X B C
0 a x b c


df['X'] = df.pop('X')
print(df)

A B C X
0 a b c x

Another, more generic, option would be to reindex, for this you can remove the columns to move last from the index and add them in the end. The advantage is that you can handle many columns at once and chose to more to different spots:

to_move = ['X']
new = df.columns.difference(to_move).to_list()+to_move
# ['A', 'B', 'C', 'X']

df = df[new]

how i can delete no needed columns from Dataframe with pandas

Try

df = df[["open", "high", "low", "close"]] # only select four columns

or

df = df.drop(df.iloc[:, 0:-5], axis=1) # delete all except last 5 columns
df = df.drop(df.iloc[:, -1], axis=1) # delete last column

inplace=True can be used in the drop calls to avoid copies:

df.drop(df.iloc[:, 0:-5], axis=1, inplace=True)
df.drop(df.iloc[:, -1], axis=1, inplace=True)


Related Topics



Leave a reply



Submit