简体   繁体   中英

Name Only Last Column of DataSet read by pandas.read_csv

I'm using the function pandas.read_csv to read data set. I want to name only the last column, I'm aware I can pass names as a list of names to all columns, but I want specifically only to last column

edit: I'm interested in the last column because it contains classification of the samples

Like the index of a DataFrame columns must be defined either by you, the file, or automatically.

Solution

I'd do it like this:

df = pd.read_csv(filename, header=None)
cols = [c for c in df.columns]
cols[-1] = my_special_name
df.columns = cols

you can read just one row using nrows parameter in order to parse the column names:

cols = pd.read_csv(filename, nrows=1).columns.tolist()

now you can use names parameter:

df = pd.read_csv(filename, skiprows=1, names=cols[:-1] + ['last_col_new_name'])

Test data:

col1,col2,col3,col4,colXXX
1,2,3,4,A
11,12,13,14,B
21,22,23,24,B

Test:

In [248]: filename
Out[248]: 'd:/temp/.data/aaa.csv'

In [249]: pd.read_csv(filename)
Out[249]:
   col1  col2  col3  col4 colXXX
0     1     2     3     4      A
1    11    12    13    14      B
2    21    22    23    24      B

In [250]: cols = pd.read_csv(filename, nrows=1).columns.tolist()

In [251]: cols
Out[251]: ['col1', 'col2', 'col3', 'col4', 'colXXX']

In [252]: df = pd.read_csv(filename, skiprows=1, names=cols[:-1] + ['last_col_new_name'])

In [253]: df
Out[253]:
   col1  col2  col3  col4 last_col_new_name
0     1     2     3     4                 A
1    11    12    13    14                 B
2    21    22    23    24                 B

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM