简体   繁体   中英

how to change the first column name in dataframe

      first        second           third           fourth
1       9             2               0                7
0       4             3               2                1
3       4             0               4               -1
2       0             1               8               -5
       first       second           third           fourth
0       7             11              1               -7
3       2             5               5               -12
1       2             4               3                1
2      -3             7              -1                7

I have this data set in the panda data fame. I wonder about how can I change the value of the first column?(this is 2 different sets of data)

ie list = ['abc','def','ghi','xyz'] i want this to replace those 0 1 2 3 in the first column in order respectively. I got something like this but it does not seem to work with multiple sets of data only work for one

for i in range(4):
   df.rename(index={i: list[i]})

this is the error I got incase it may helps TypeError: unhashable type: 'list'

PS I already sorted the data descending using the 'first' column so the index 0 1 2 3 is mixed up

my expected out put would be something like

first        second           third           fourth
def       9             2               0                7
abc       4             3               2                1
xyz       4             0               4               -1
ghi       0             1               8               -5
       first       second           third           fourth
abc       7             11              1               -7
xyz       2             5               5               -12
def       2             4               3                1
ghi      -3             7              -1                7

Use map

d = dict(zip([0,1,2,3],['abc','def','ghi','xyz']))
df.index= df.index.map(d)

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