python - 从 csv 列中提取最常见的组合

标签 python python-3.x pandas numpy

我一直在尝试从 csv 文件中提取起始站和结束站最常见的组合,我遇到的问题是我正在提取所有这些组合。当试图只提取组合最多的一个时,我只得到一个数字?

def station_stats(df):

frequent_stations = df.groupby(['Start Station'])['End Station'].value_counts().mode
print('Most frequent start and end station: ', frequent_stations)

下面是我当前输出的片段:

Most frequent start and end station:  <bound method Series.mode of Start Station                 End Station

2112 W Peterson Ave           Warren Park East                    8
                          Clark St & Berwyn Ave               5
                          Washtenaw Ave & Lawrence Ave        4

最佳答案

IIUC

df.groupby(['Start Station','End Station']).size().nlargest(1)

关于python - 从 csv 列中提取最常见的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848454/

相关文章:

python - 在 Python 中,如何输入提示 'has attribute' ?

python - 基于字符串值创建分类列

python - Google BigQuery Schema 冲突(pyarrow 错误)与使用 load_table_from_dataframe 的数字数据类型

python - RabbitMQ 消息丢失

python - 来自 joblib 的多处理不并行化?

python-3.x - 将 cx_freeze 与 pythoncom 一起使用时导入错误

python - 如何从python中的字符串获取索引位置而不产生重复结果

python - Pandas 爆炸 - 无法从重复的轴重新索引

python 嵌套列表和字典,无法访问和设置

python - 在 pytest 参数化中使用类型错误消息