[英]Selecting rows in Pandas Panel
Is there a way to select rows from across a Pandas panel? 有没有办法从Pandas面板中选择行?
For instance in the following example could I get all rows where Job == "B" across dataframes? 例如,在以下示例中,我可以获取跨数据框的Job ==“B”的所有行吗? In a regular dataframe I know I could do this using
在常规数据框架中,我知道我可以使用
df1[df1["job"] == "A"]
df1 [df1 [“job”] ==“A”]
but I'm unsure how to do this in Pandas panels without loops. 但我不确定如何在没有循环的Pandas面板中这样做。
df1 = pd.DataFrame({"job":["A", "B", "C", "D"],"date":["DateA1", "DateB1", "DateC1", "DateD1"]})
df2 = pd.DataFrame({"job":["B", "C", "D", "E"],"date":[ "DateB2", "DateC2", "DateD2", "DateE2"]})
p = pd.Panel({"df1":df1, "df2":df2})
My question might be a duplicate of this one 我的问题可能与此问题重复
It might be more convenient (at least to me :-)) to work with 2D df rather than 3D panel. 使用2D df而不是3D面板可能更方便(至少对我来说:-))。
df1 = pd.DataFrame({"job":["A", "B", "C", "D"],"date":["DateA1", "DateB1", "DateC1", "DateD1"]})
df2 = pd.DataFrame({"job":["B", "C", "D", "E"],"date":[ "DateB2", "DateC2", "DateD2", "DateE2"]})
p = pd.Panel({"df1":df1, "df2":df2})
frame = p.to_frame()
Out[12]:
df1 df2
major minor
0 date DateA1 DateB2
job A B
1 date DateB1 DateC2
job B C
2 date DateC1 DateD2
job C D
3 date DateD1 DateE2
job D E
res = frame.unstack('minor').stack(level=0)
Out[13]:
minor date job
major
0 df1 DateA1 A
df2 DateB2 B
1 df1 DateB1 B
df2 DateC2 C
2 df1 DateC1 C
df2 DateD2 D
3 df1 DateD1 D
df2 DateE2 E
res.loc[res['job'] == 'B', :]
Out[14]:
minor date job
major
0 df2 DateB2 B
1 df1 DateB1 B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.