Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从df中写入选择和(列)、计数(其他列)_Python_Pandas - Fatal编程技术网

Python 如何从df中写入选择和(列)、计数(其他列)

Python 如何从df中写入选择和(列)、计数(其他列),python,pandas,Python,Pandas,我是来自pyspark的pandas初学者,希望用pandas编写以下sql查询: select sum(col) as sum_col, count(other_col) as count_other_col from pandas_df 我希望它返回一个新的数据帧 谢谢 我发现返回计数或总数但从未在新数据帧中出现的代码可能使用: print(df.assign(col=df['col'].sum(), other_col=df['other_col'].size).iloc[[0]])

我是来自pyspark的pandas初学者,希望用pandas编写以下sql查询:

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

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

谢谢

我发现返回计数或总数但从未在新数据帧中出现的代码可能使用:

print(df.assign(col=df['col'].sum(), other_col=df['other_col'].size).iloc[[0]])
可能使用:

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'])
试试这个:

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


预期的输出是什么?@U10 Forward预期的输出应该是一个新的数据帧,它有两列名为sum\u col,count\u other\u col,一行有各自操作的值。“各自操作的值”它们是什么?一个操作是
sum
,另一个是
count
。预期的输出是什么?@U10 Forward预期的输出应该是一个新的数据帧,它有两列名为sum\u col,count\u other\u col,一行有来自各自操作的值。“来自各自的操作”它们是什么?一个操作是
sum
,另一个是
count
。这样做只会返回相同的数据帧?实际上只返回相同数据帧的第一行dataframe@LawrenceVo不,是总和和size@LawrenceVo你不能只做agg=pandas_df.agg({'col':'sum','other_col':'count'))然后从其索引中获取您的值?因此,
count\u other\u col
将是
agg.loc['b']
?Ahhh-0.20.0中的新版本-您知道我们现在达到了0.25.0…)这样做只返回相同的数据帧?实际上只返回相同数据帧的第一行dataframe@LawrenceVo不,是总和和size@LawrenceVo你能不能不做
agg=pandas_df.agg({'col':'sum','other_col':'count'})
然后从它的索引中得到你的值?因此,
count\u other\u col
将是
agg.loc['b']
?Ahhh-0.20.0中的新版本-您知道我们现在达到了0.25.0…)这是一个不错的解决方案,但希望能有更好的结果。如果我找不到更好的解决方案,我可能会使用这个。这不是一个坏的解决方案,但我希望它能翻译得更好。如果我找不到更好的方法,我可能会使用它。这会不会被弃用?我可能会考虑这个解决方案。“LaWrEnEvo”开发人员投入了大量的精力来规范<代码> > AGG用于数据文件、GROPBY和滚动等。据我所知,这种方法是他们认为的习惯用法。如果你有反对的证据,请分享。@brentertainer不知道要完全诚实。谢谢在前进的道路上,这不是很不受欢迎吗?我可能会考虑这个解决方案。“LaWrEnEvo”开发人员投入了大量的精力来规范<代码> > AGG用于数据文件、GROPBY和滚动等。据我所知,这种方法是他们认为的习惯用法。如果你有反对的证据,请分享。@brentertainer不知道要完全诚实。谢谢