我正在尝试通过Pandas中的一组索引值调用一个值。当我使用这种方法创建垃圾箱时,我可以毫无问题地调用索引值:
import numpy as np
import pandas as pd
df=pd.DataFrame(100*np.random.uniform(0, 1, size=100),columns=['randoms'])
df['the']='the'
df['another']=100*np.random.uniform(0, 1, size=100)
bins=[0,50,100]
labels=['0-50','51-100']
df['cat']=df.randoms.apply(lambda x: '0-50' if x<=50 else '51-100')
x=df.groupby(['the','cat']).quantile(np.arange(0,1.1,.1))
x.loc['the','0-50']
然而,我意识到我的binning变得更复杂,我应该使用pd.cut方法。但是当我这样做时,我收到以下错误:
import numpy as np
import pandas as pd
df=pd.DataFrame(100*np.random.uniform(0, 1, size=100),columns=['randoms'])
df['the']='the'
df['another']=100*np.random.uniform(0, 1, size=100)
bins=[0,50,100]
labels=['0-50','51-100']
df['cat']=pd.cut(df.randoms,bins,labels=labels)
x=df.groupby(['the','cat']).quantile(np.arange(0,1.1,.1))
x.loc['the','0-50']
KeyError: 'the label [0-50] is not in the [columns]'
然而,这些在第二种方法中工作正常:
x.loc['the']
x.loc['the','0-50',0]
这里发生了什么?