繁体   English   中英

如何通过使用 pandas 查找另一个 DataFrame 在 DataFrame 列中创建列表?

[英]How to create lists in a DataFrame column by looking up another DataFrame using pandas?

我有一个来自DataFrame的 DataFrame,名为df ,它有多个列(仅在下面显示 3)和 90,000 行:

        Key        Date     Rating
0      123abc   08/19/2015    A
1      456def   04/23/2013    B-
2      123abc   06/10/2012    C
3      789ghi   01/04/2017    B
.        .           .        .
.        .           .        .
90000  999zzz   12/12/2012    D

我想创建一个单独的DataFramedf_ratings ,它有两列: KeyRating List df_ratings中, Key列必须是唯一的,并且Rating List列应包含针对df中的Key出现的所有Ratings的列表。

        Key       Rating List
0      123abc     ['A', 'C']
1      456def       ['B-']
2      789ghi     ['B', 'D']
.        .            .
.        .            .
30000  999zzz   ['A', 'C+', 'D']

到目前为止我使用的方法是:

df_zip = list(zip(df['Key'], df['Rating']))

def dfRatingsList(row):
    rating = []
    for x, y in df_zip:
        if row['Key'] == x:
            rating.append(y)
    return rating

df_ratings = pd.DataFrame(df['Key'].unique(), columns=['Key'])
df_ratings = df_ratings.fillna('NULL')
df_ratings['Rating List'] = df_ratings.apply(dfRatingsList, axis=1)

鉴于我的数据集的大小,这需要几个小时才能运行。 我怎样才能加快这个过程/改进我的代码?

尝试这个:

df = df.groupby(by=['Key'], as_index=False).agg({'Rating': list})
print(df)

      Key        Rating
0  123abc  [A, A, A, A]
1  123def           [C]
2  456def          [B-]
3  789ghi           [B]
4  999zzz           [D]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM