Python索引提供空数据帧错误

2024-06-12 04:44:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,尝试在Python中使用Pandas对数据进行索引和切片,但这给了我一个“空数据帧”的错误。 以下是我的python代码行:

import pandas as pd
from scipy import stats

df = pd.read_csv(filename,delim_whitespace=True, header=None, names=['year','month','lat','lon','aod','toa','sfc'])
df.set_index('year','month','lat','lon','aod','toa','sfc', inplace=True, drop=True)
idx = pd.IndexSlice

tmp = df.loc[idx[2011,3,10.0,30.0,:,:,:],]
print(tmp)

这给了我以下错误:

Empty DataFrame
Columns: []
Index: [(2011, 3, 10.0, 30.0, 1.031, 228.524, 987.44), (2011, 3, 10.0, 30.0, 0.28300000000000003, 202.205, 1036.77), (2011, 3, 10.0, 30.0, 0.45899999999999996, 214.261, 1054.85), (2011, 3, 10.0, 30.0, 0.35100000000000003, 201.296, 1064.03), (2011, 3, 10.0, 30.0, 0.15, 195.709, 1073.97), (2011, 3, 10.0, 30.0, 0.204, 201.37599999999998, 1061.8), (2011, 3, 10.0, 30.0, 0.39899999999999997, 195.92700000000002, 1041.49), (2011, 3, 10.0, 30.0, 0.612, 203.899, 1018.99)]    

这些行给出的结果没有带空数据帧错误的切片。 如果有人指出解决办法,我将不胜感激

多谢各位


Tags: 数据importtruedf错误切片yearpd