嗨,我有一个数据框,想要提取特定行的数据。
我的密码
config = pd.read_excel('milo_config.xlsx',sheet_name='B2WL3')
data = config.set_index('IPv6 Address')
config df看起来像
Block Floor IPv6 Address Serial ID
0 B2W L3 fd00::212:4b00:1957:defa 25.0
1 B2W L3 fd00::212:4b00:1957:e315 13.0
2 B2W L3 fd00::212:4b00:1957:d661 16.0
3 B2W L3 fd00::212:4b00:1957:da6b 17.0
4 B2W L3 fd00::212:4b00:1957:de87 21.0
5 B2W L3 fd00::212:4b00:1957:e314 12.0
6 B2W L3 fd00::212:4b00:1957:e37e 18.0
7 B2W L3 fd00::212:4b00:1957:da2e 23.0
8 B2W L3 fd00::212:4b00:1957:e32c 22.0
9 B2W L3 fd00::212:4b00:1957:f00e 26.0
10 B2W L3 fd00::212:4b00:1957:da4b 27.0
和数据df看起来像
Block Floor Serial ID
IPv6 Address
fd00::212:4b00:1957:defa B2W L3 25.0
fd00::212:4b00:1957:e315 B2W L3 13.0
fd00::212:4b00:1957:d661 B2W L3 16.0
fd00::212:4b00:1957:da6b B2W L3 17.0
fd00::212:4b00:1957:de87 B2W L3 21.0
fd00::212:4b00:1957:e314 B2W L3 12.0
fd00::212:4b00:1957:e37e B2W L3 18.0
fd00::212:4b00:1957:da2e B2W L3 23.0
fd00::212:4b00:1957:e32c B2W L3 22.0
当我尝试使用特定地址/索引提取行时
data.loc['fd00::212:4b00:1957:eb84']
我收到一个错误
KeyError: 'fd00::212:4b00:1957:eb84'
我不明白为什么,我曾经尝试过使用相同的方式提取行数据。我不明白为什么现在不发生这种情况。我想念什么吗?
谢谢。
最佳答案
索引值中存在空格,因此请在通过strip
选择之前将其删除:
df.index = df.index.str.strip()
要么:
df = df.rename(lambda x: x.strip())
关于python - 通过给出错误的索引获取数据帧的行数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58657190/