【发布时间】:2019-10-10 20:07:28
【问题描述】:
让我们考虑以下数据框:
import pandas as pd
d = {'col1': [1, 2, 3], 'col2': [3, 4, 5]}
df=pd.DataFrame(data=d)
如果我想访问pandas系列的第一个元素df['col1'],我可以直接去df['col1'][0]。
但是我怎样才能访问这个系列中的 last 元素呢?
我试过df['col1'][-1],它返回以下错误:
密钥错误:-1L
我知道我可以选择像df['col1'][len(df)-1] 这样的东西,但为什么反向索引在这里是不可能的?
【问题讨论】: