pandas根据行名称、列名称,得到对应的下标

使用get_loc()的函数可以根据名称,得到下标索引

示例代码

import pandas as pd

df = pd.DataFrame(
    [[1, 2, 3], [2, 3, 4], [4, 5, 6], [7, 8, 9]],
    index=['a', 'b', 'c', 'd'], columns=['A', 'B', 'C']
)

print(df)

得到的df如下:

   A  B  C
a  1  2  3
b  2  3  4
c  4  5  6
d  7  8  9

然后使用:

print(df.index.get_loc("d"))
print(df.columns.get_loc("A"))

得到结果:

3
0

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>