【问题标题】:Python - Group rows in list in pandas dataframePython - 在熊猫数据框中对列表中的行进行分组
【发布时间】:2018-04-20 02:37:53
【问题描述】:

我有一个这样的数据框:

long lat Place
-6.779 61.9 奥胡斯
-6.790 62.0 奥胡斯
54.377 24.4 扎比
38.834 9.0 亚的斯
35.698 9.2 亚的斯
    Is it possible to transform the dataframe into a format like below?

Office    long + lat
Aarhus     [[-6.779,61.9], [-6.790,62.0]]
Dhabi      [[54.377]]
Addis      [[38.834,9.0], [35.698,9.2]]

我尝试了不同的方法,但仍然无法解决这个问题。这是 我试图为每个不同的位置值获取一个列表:

df2["index"] = df2.index
df2["long"]=df2.groupby('index')['long'].apply(list)
list 1= [] 
for values in ofce_list:
    if df['Office'].any() == values:
        list1.append(df.loc[df['Office'] == values, 'long'])

    But this returned a series in a list instead which is not desired. Please help. Thank you so much.

【问题讨论】:

    标签: python list pandas group-by


    【解决方案1】:
     df.groupby('Place')[['long','lat']].apply(lambda x :x.values.tolist()).\
          reset_index(name='long + lat')
    Out[1380]: 
        Place                       long + lat
    0  Aarhus  [[-6.779, 61.9], [-6.79, 62.0]]
    1   Addis   [[38.834, 9.0], [35.698, 9.2]]
    2   Dhabi     [[54.376999999999995, 24.4]]
    

    【讨论】:

    • 别忘了.reset_index(name='long + lat')
    猜你喜欢
    • 2017-10-15
    • 2017-12-13
    • 2013-07-30
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多