我有一个带有(p,t)元组形式的multiindex的DataFrame,其中p和t是浮点数。当我尝试通过切片(idx=IndexSlice
)选择某个p值时,我收到以下错误:
df.loc[idx[1.5,:]]
/usr/local/lib/python2.7/dist-packages/pandas-0.14.0rc1-py2.7-linux-x86_64.egg/pandas/core/index.py:496:
FutureWarning: scalar indexers for index type MultiIndex should be integers and not floating point
我有办法解决这个问题吗?
输入数据框:
Pump Time
10.0 -10.60 0.000005
-10.59 0.000031
-10.58 0.000007
-10.57 -0.000020
-10.56 -0.000000
-10.55 0.000005
-10.54 -0.000013
-10.53 -0.000049
-10.52 -0.000031
-10.51 -0.000041
-10.50 0.000022
-10.49 -0.000045
-10.48 -0.000070
-10.47 -0.000025
-10.46 0.000002
...
-0.05 4.05 0.000610
6.05 0.000443
8.05 0.000318
10.05 0.000380
12.05 -0.000063
14.05 0.000578
16.05 0.000236
18.05 0.000472
20.05 0.001628
40.05 0.000243
60.05 0.000426
80.05 0.000361
100.05 0.000693
120.05 0.000478
140.05 0.000398
Name: p1Up, Length: 4400, dtype: float64
期望的输出:
Pump Time
-0.05 4.05 0.000610
6.05 0.000443
8.05 0.000318
10.05 0.000380
12.05 -0.000063
14.05 0.000578
16.05 0.000236
18.05 0.000472
20.05 0.001628
40.05 0.000243
60.05 0.000426
80.05 0.000361
100.05 0.000693
120.05 0.000478
140.05 0.000398
df.info()抛出:
MultiIndex: 4400 entries, (10.0, -10.6) to (-0.05, 140.05)
Data columns (total 1 columns):
p1Up 4400 non-null float64
dtypes: float64(1)
构造是通过在for循环中连接来迭代不同的p值:
time = (extracted from a file)
lb = [(p,t) for t in time]
ind = pd.MultiIndex.from_tuples(lb, names=['Pump','Time'])
col = ['p1Up','p1Down']
data = np.concatenate((p1up,p1down),axis=1)
# Build dataframes
temp = pd.DataFrame(data, index=ind, columns=col)
df = pd.concat([df,temp])
答案 0 :(得分:2)