Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x DataFrame选择所有列都具有最大出现频率的列值_Python 3.x_Dataframe_Aggregate - Fatal编程技术网

Python 3.x DataFrame选择所有列都具有最大出现频率的列值

Python 3.x DataFrame选择所有列都具有最大出现频率的列值,python-3.x,dataframe,aggregate,Python 3.x,Dataframe,Aggregate,我正在尝试从具有最大频率的列值出现计数中选择一个值。当具有相同频率的多个值恰好达到最大值时,会出现边缘情况 我要做的是用该值所属的另一列行的值的最大聚合和来选取该值 max_cur_freq = df[df['currency'] != '']['currency'].mode().tolist() biggest_amount = 0.0 biggest_amount_currency = '' for cur in max_cur_freq: if df[df['currency']

我正在尝试从具有最大频率的列值出现计数中选择一个值。当具有相同频率的多个值恰好达到最大值时,会出现边缘情况

我要做的是用该值所属的另一列行的值的最大聚合和来选取该值

max_cur_freq = df[df['currency'] != '']['currency'].mode().tolist()
biggest_amount = 0.0
biggest_amount_currency = ''
for cur in max_cur_freq:
    if df[df['currency'] == cur]['amount'].agg('sum') > biggest_amount:
       biggest_amount_currency = cur
       biggest_amount = df[df['currency'] == cur]['amount'].agg('sum')

# assigns the currency with the largest sum amount as the values for column common_currency
df['common_currency'] = biggest_amount_currency
因此,代码选择行中最大金额为
货币
,其中
货币
存在于
数据框中

我想知道做这件事最好的方法是什么

PS.示例
数据帧

   currency   amount
50      CAD   410.85
51      CAD   1441.68
53      CAD   1330.33
17625   JPY   2797856.0
17663   JPY   1440.0
17664   JPY   1445.33
16734   CNY   27840.00
54546   CNY   273269.53
17654   GBP   384.0
17655   GBP   526.0
16732   CHF   474.7
16733   CHF   195173.3

数据帧的每一列实际上是一个系列

现在,在一个系列中,如果您想知道哪个元素在大多数时间出现,那么请使用.value_counts().idxmax()

如果您想知道最大频率,请使用.value_counts().max()