1

I have a SQL query:

pd.read_sql_query("""SELECT UvTab.A, UvTab.Uv,
                            IFNULL(DvTab.Dv, 0) AS Dv 
                                FROM
                                    (
                                        SELECT A, COUNT(*) AS Uv FROM B
                                            WHERE Vtype = 2 GROUP BY A
                                    ) AS UvTab
                                LEFT JOIN 
                                    (
                                        SELECT A, COUNT(*) AS Dv FROM B
                                            WHERE Vtype = 3 GROUP BY A
                                    ) AS DvTab
                                ON UvTab.A = DvTab.A
                            """, conn)

And my goal is to get the same result but using only pandas' methods. What I obtained is:

UvTab = B.loc[B.Vtype == 2].groupby("A").size()
UvTab = pd.DataFrame({'A' : UvTab.index, 'Uv' : UvTab.values})
DvTab = B.loc[B.Vtype == 3].groupby("A").size()
DvTab = pd.DataFrame({'A' : DvTab.index, 'Dv' : DvTab.values})
df = pd.merge(UvTab, DvTab, how='left', on='A')
df['Dv'] = df['Dv'].fillna(0)

And it seems to be fine. But is this the simpliest and the best way to represent the query?

1 Answer 1

2

One idea is aggregate sum for count matching and then use DataFrame.join:

UvTab = (B.Vtype == 2).astype(int).groupby(B["A"]).sum().reset_index(name='Uv')
DvTab = (B.Vtype == 3).astype(int).groupby(B["A"]).sum().to_frame('Dv')
df = UvTab.join(DvTab, on='A').fillna({'DV':0})

Or alternative with merge:

UvTab = (B.Vtype == 2).astype(int).groupby(B["A"]).sum().reset_index(name='Uv')
DvTab = (B.Vtype == 3).astype(int).groupby(B["A"]).sum().reset_index(name='Dv')
df = UvTab.merge(DvTab, on='A', how='left').fillna({'DV':0})
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.