简体   繁体   中英

How to write a select sum(col), count(other_col) form pandas_df

I am a beginner to pandas coming from pyspark and would like to write the following sql query in pandas:

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

I would like this to return a new dataframe.

Thanks.

I've found code that returns the count or total but never in a new dataframe

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM