Selecting Rows in Pandas Panel

Is there a way to select rows from the Pandas panel?

For example, in the following example, I can get all rows where Job == "B" for all data frames? In a regular dataframe, I know I can do this using

df1 [df1 ["job"] == "A"]

but I'm not sure how to do this in Pandas panels without loops.

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

Logical masking in panels

+3


source to share


1 answer


It might be more convenient (at least for me :-)) to work with a 2D df rather than a 3D panel.



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

      

+1


source







All Articles