python 如何根据dataframe的列值形成一个列表?

roqulrg3  于 2023-02-07  发布在  Python
关注(0)|答案(1)|浏览(217)

我有以下 Dataframe :

df = pd.DataFrame({'lines': [[0], [98], [64, 1], [64, 65], [64, 69]],
                   'stations': [[0, 1], [0, 1], [0, 3, 1], [0, 3, 1], [0, 3, 1]]})`

print(df)

      lines   stations
0       [0]     [0, 1]
1      [98]     [0, 1]
2   [64, 1]  [0, 3, 1]
3  [64, 65]  [0, 3, 1]
4  [64, 69]  [0, 3, 1]

我想创建两(2)个列表;lines_liststations_list,它们具有以下格式:

lines_list= [[[0, 98]], [[64], [1,65,69]]]
stations_list= [[0,1], [0,3,1]]

我尝试过这个解决方案:

df['stations'] = df['stations'].apply(tuple)
grouped = df.groupby('stations')['lines'].apply(list)

#for lines_list
lines_list = [group for group in grouped]

#for stations_list
stations_list = grouped.index.tolist()
stations_list = [list(item) for item in stations_list]

对于stations_list,它工作,因为我得到了预期的输出。但是,我没有得到我需要的lines_list输出。相反,我得到了[[[0], [98]], [[64, 1], [64, 65], [64, 69]]]。关于如何修复这个问题有什么建议吗?

yjghlzjz

yjghlzjz1#

您需要对分组对象应用自定义函数。该函数会将每个组中的列表合并为一个列表

import pandas as pd 
df = pd.DataFrame({'lines': [[0], [98], [64, 1], [64, 65], [64, 69]],
                   'stations':[[0, 1], [0, 1], [0, 3, 1], [0, 3, 1], [0, 3, 1]]})
                   

df['stations'] = df['stations'].apply(tuple)
grouped = df.groupby('stations')['lines'].apply(list)

def combine_lists(group):
    result = []
    for sublist in group:
        result += sublist
    return [result]

lines_list = grouped.apply(combine_lists).tolist()

stations_list = grouped.index.tolist()
stations_list = [list(item) for item in stations_list]

print(lines_list)
print(stations_list)

相关问题