从列中的多个值获取组成数据帧

我有一个示例数据框,其中有两个值(总线和汽车+小巴)在mode column我有两个问题,第一个是我怎么能把它分解成两个数据帧具有单个值,如下所示。第二个问题是因为我有一个数据框的列表,其中一些发生在input格式中,我怎样才能写出一个条件语句他们。从列中的多个值获取组成数据帧

输入

      Dest   a  b  c 

Orig Variable Time Mode

1 x y Bus 5.00 17.32 12.61

Car+Minibus 0.87 15.34 12.01

2 x y Bus 5.00 14.72 10.15

Car+Minibus 10.47 3.03 11.05

3 x y Bus 14.72 5.00 15.98

Car+Minibus 11.64 11.25 2.08

4 x y Bus 15.15 14.62 5.94

Car+Minibus 12.02 9.25 5.80

输出:

      Dest   a  b  c 

Orig Variable Time Mode

1 x y Bus 5.00 17.32 12.61

2 x y Bus 5.00 14.72 10.15

3 x y Bus 14.72 5.00 15.98

4 x y Bus 15.15 14.62 5.94

Dest a b c

Orig Variable Time Mode

1 x y Car+Minibus 0.87 15.34 12.01

2 x y Car+Minibus 10.47 3.03 11.05

3 x y Car+Minibus 11.64 11.25 2.08

4 x y Car+Minibus 12.02 9.25 5.80

回答:

我相信你需要通过boolean indexing检查MultiIndex和过滤第四级:

mask = df.index.get_level_values(3) == 'Bus' 

df1 = df[mask]

df2 = df[~mask]

但如果想用的DataFrame列表工作s:

dfs = [df11,df12,df13] 

for df in dfs:

mask = df.index.get_level_values(3) == 'Bus'

df1 = df[mask]

print (df1)

df2 = df[~mask]

print (df2)

以上是 从列中的多个值获取组成数据帧 的全部内容, 来源链接: utcz.com/qa/262962.html

回到顶部