[英]how to replace index of columns and rows in Pandas DataFRame
I am trying to get confident with Pandas, I would like to understand how can I use one generic DataFrame column as row index and how can I delete it from the matrix. 我想对Pandas充满信心,我想了解如何使用一个通用DataFrame列作为行索引以及如何将其从矩阵中删除。
Say that I have a matrix such as 假设我有一个矩阵,例如
a b c d e
11 2 1 0 aa 2
22 1 1 0 bb 1
33 4 b 3 cc 9
44 5 2 2 dd 5
55 2 9 8 ee 6
in which the first column and the first row are not data but indexes. 其中第一列和第一行不是数据而是索引。 I would like the d column ('aa', 'bb', 'cc', 'dd', 'ee') to be the row index, I don't care of the original row index and I don't want the 'd' column to be a matrix column.
我希望d列(“ aa”,“ bb”,“ cc”,“ dd”,“ ee”)成为行索引,我不在乎原始行索引,并且我不想'd'列为矩阵列。 Long story short I would like a matrix such as
长话短说,我想要一个矩阵,例如
a b c e
aa 2 1 0 2
bb 1 1 0 1
cc 4 b 3 9
dd 5 2 2 5
ee 2 9 8 6
in which 'a', 'b', 'c', 'e' and 'aa', 'bb', 'cc', 'dd', 'ee' are column and row indexes respectively. 其中,“ a”,“ b”,“ c”,“ e”和“ aa”,“ bb”,“ cc”,“ dd”,“ ee”分别是列索引和行索引。 How can I do this job?
我该怎么做?
You can use set_index
: 您可以使用
set_index
:
print df
a b c d e
11 2 1 0 aa 2
22 1 1 0 bb 1
33 4 b 3 cc 9
44 5 2 2 dd 5
55 2 9 8 ee 6
print df.set_index('d')
a b c e
d
aa 2 1 0 2
bb 1 1 0 1
cc 4 b 3 9
dd 5 2 2 5
ee 2 9 8 6
Or with reset index name
: 或使用重置
index name
:
df = df.set_index('d')
df.index.name= None
print df
a b c e
aa 2 1 0 2
bb 1 1 0 1
cc 4 b 3 9
dd 5 2 2 5
ee 2 9 8 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.