Python/Pandas : If Column has multiple values, 转换为列表中具有多个值的单行

标签 python list pandas dataframe apply

在我的 DataFrame 中,我有许多具有不同 KeyValue_String 的相同 AutoNumber 实例。我想将这些实例转换为单行,其中 KeyValue_String 是由多个唯一值组成的列表。

    AutoNumber KeyValue_String  ReferralType                      Description
0        50899              DD             3                       Web Search
1        50905          Cheque             1            Gatestone Collections
2        50906              DD             2          Centum Mortgage Brokers
3        50907          Cheque             1     Financial Debt Recovery Ltd.
4        50908              DD             2          Centum Mortgage Brokers
5        50909              DD             2          Centum Mortgage Brokers
6        50910          Cheque             1      Allied International Credit
7        50911          Cheque             1              D&A Collection Corp
8        50912          Cheque             1            Gatestone Collections
9        50913          Cheque             1     Financial Debt Recovery Ltd.
10       50914          Cheque             3  Existing Customer - Refinancing
11       50914              DD             3  Existing Customer - Refinancing
12       50915          Cheque             1            Gatestone Collections
13       50916          Cheque             3  Existing Customer - Refinancing
14       50916          Cheque             3  Existing Customer - Refinancing

所需的输出如下所示,但我想保留所有其他列

      AutoNumber KeyValue_String
0          50899            DD
1          50905        Cheque
2          50906            DD
3          50907        Cheque
4          50908            DD
5          50909            DD
6          50910        Cheque
7          50911        Cheque
8          50912        Cheque
9          50913        Cheque
10         50914    [Cheque, DD]
11         50915        Cheque
12         50916        Cheque
13         50917        Cheque
14         50918        Cheque

最佳答案

如果我理解正确的话,您可以选择使用 groupby , transform ,和unique

df['KeyValue_String'] = df.groupby('AutoNumber').KeyValue_String.transform('unique')

然后,您可以删除重复项,假设如注释中所述,具有相同自动编号的行除 KeyValue_String 之外还包含重复信息。

df = df.drop_duplicates(subset='AutoNumber')

我建议如果您想要数组,请将列中的所有内容保留为数组,并且不要花费精力将混合类型放入列中,无论如何,这都会更难使用。

演示

>>> df
    AutoNumber KeyValue_String
0        50899              DD
1        50905          Cheque
2        50906              DD
3        50907          Cheque
4        50908              DD
5        50909              DD
6        50910          Cheque
7        50911          Cheque
8        50912          Cheque
9        50913          Cheque
10       50914          Cheque
11       50914              DD
12       50915          Cheque
13       50916          Cheque
14       50916          Cheque

>>> df['KeyValue_String'] = df.groupby('AutoNumber').KeyValue_String.transform('unique')

>>> df.drop_duplicates(subset='AutoNumber')

    AutoNumber KeyValue_String
0        50899            [DD]
1        50905        [Cheque]
2        50906            [DD]
3        50907        [Cheque]
4        50908            [DD]
5        50909            [DD]
6        50910        [Cheque]
7        50911        [Cheque]
8        50912        [Cheque]
9        50913        [Cheque]
10       50914    [Cheque, DD]
12       50915        [Cheque]
13       50916        [Cheque]

关于Python/Pandas : If Column has multiple values, 转换为列表中具有多个值的单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42491753/

相关文章:

python - 来自 PIL 图像的 32 位 RGBA numpy 数组

python - 如何使 wx.TextEntryDialog 变大和可调整大小

python - 如何在 paramiko 连接中临时添加 host_key

bash - 如何从 bash 中的循环创建字符串列表?

python - 不同大小的笛卡尔积

python - 在 pandas 数据框列(又名 pd.series)中查找数组元素位置

python - 使用 dtype float 将 pandas.Multiindex 转换为 numpy.ndarray

javascript - AngularJS 不存在 'Access-Control-Allow-Origin' header

list - 将元素添加到 Map 内的 List (ImmutableJS)

python - for循环获取 Pandas 中的数据框