【问题标题】:How to set unique column ID based on other colums via dictionary to every single row in DataFrame如何通过字典为DataFrame中的每一行设置基于其他列的唯一列ID
【发布时间】:2021-03-04 17:26:44
【问题描述】:

我有一个包含大约 30,000 多行和 9 个唯一列的数据框。我想为每一行生成一个唯一的 ID 并将其放在一个新列中。例如:

old_car_brand continent country     income_level occupation    new_car_brand
Tesla         Asia      Indonesia   Medium       Businessman   Subaru
Ferrari       Asia      Brunei      Medium       Businessman   Tesla

从上面的数据框中,我想创建另一列,其 ID 是基于另一列生成的。我已经有一本像下面这样的字典:

ID_Dict = {'Tesla' : 'T001',
           'Ferrari' : 'F001',
           'Asia' : 'XASIAX',
           'Brunei' : 'BRUN',
           'Indonesia' : 'INDN'
          }

我尝试使用 df.itertuples() 迭代每一行并检索所需的信息,但每当我将值写入列时,每一行的值都以某种方式设置相同。下面是我写的示例。

for row in Test_DF.itertuples():
   a = ID_Dict[row.oldCar]
   b = ID_Dict[row.Continent]
   c = ID_Dict[row.Country]
   d = ID_Dict[row.newCar]
   Test_DF['sales_id'] = a+b+c+d

sales_id 列存在,但它将最后一行 sales_ID 设置为每一行,如下所示:

old_car_brand continent country     income_level occupation    new_car_brand  sales_id
Tesla         Asia      Indonesia   Medium       Businessman   Ferrari        F001XASIAXBRUNT001
Ferrari       Asia      Brunei      Medium       Businessman   Tesla          F001XASIAXBRUNT001

但我希望结果如下(基于字典):

old_car_brand continent country     income_level occupation    new_car_brand  sales_id
Tesla         Asia      Indonesia   Medium       Businessman   Ferrari        T001XASIAXINDNF001
Ferrari       Asia      Brunei      Medium       Businessman   Tesla          F001XASIAXBRUNT001

我做错了什么?有没有办法让我去每一行,根据列的值生成 id,将值链接到字典,并将 ID 放在列中?先谢谢啦~

【问题讨论】:

  • 斯巴鲁不在ID_Dict字典中

标签: python pandas dataframe dictionary


【解决方案1】:

这个单线就可以完成这项工作:

df['sales_id'] = df.apply(lambda row: ID_Dict[row['old_car_brand']] + ID_Dict[row['continent']] + ID_Dict[row['country']] + ID_Dict[row['new_car_brand']], axis = 1)

循环总是很耗时。

您在数据框和循环中使用了不同的列名。我相信这是一个错字。在这里,我假设列名是您提供的示例数据框中的列名。此外,我假设条目“Subaru”也出现在字典中,值为“S001”。

df 更改为您的数据框的名称。

样本输出:

+----+-----------------+-------------+-----------+----------------+--------------+-----------------+--------------------+
|    | old_car_brand   | continent   | country   | income_level   | occupation   | new_car_brand   | sales_id           |
|----+-----------------+-------------+-----------+----------------+--------------+-----------------+--------------------|
|  0 | Tesla           | Asia        | Indonesia | Medium         | Businessman  | Subaru          | T001XASIAXINDNS001 |
|  1 | Ferrari         | Asia        | Brunei    | Medium         | Businessman  | Tesla           | F001XASIAXBRUNT001 |
+----+-----------------+-------------+-----------+----------------+--------------+-----------------+--------------------+

【讨论】:

    猜你喜欢
    • 2023-01-05
    • 1970-01-01
    • 2023-03-15
    • 2017-10-28
    • 2018-01-05
    • 1970-01-01
    • 2022-01-01
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多