如何编写SELECT SUM(col),count(其他_COL)表单pandas_df



我是来自pyspark的熊猫的初学者,想在pandas中写下以下SQL查询:

select sum(col) as sum_col, count(other_col) as count_other_col from pandas_df

我希望它返回一个新的数据框。

谢谢。

我找到了返回计数或总数但从未在新数据框架中返回的代码

df.agg({'col': 'sum', 'other_col': 'count'}).rename({'col': 'sum_col', 'other_col': 'count_other_col'}).to_frame().T

也许使用:

print(df.assign(col=df['col'].sum(), other_col=df['other_col'].size).iloc[[0]])

尝试以下:

df = pd.DataFrame([[pandas_df.col.sum(),pandas_df.other_col.count()]], columns=['sum','count'])

最新更新