Python 应用映射在dataframe中创建新列

Python 应用映射在dataframe中创建新列,python,pandas,Python,Pandas,我有一本这样的字典 labels = { 0:['replaced scanner', 'scanner has been replaced', 'replaced the scanner', 'scanner was replaced', 'replaced scanner and tested', 'i replaced the scanner', 'deployed replacement scanner', 'replaced scanner with a new one', '

我有一本这样的字典

labels = {
    0:['replaced scanner', 'scanner has been replaced', 'replaced the scanner', 'scanner was replaced', 'replaced scanner and tested', 'i replaced the scanner', 'deployed replacement scanner', 'replaced scanner with a new one', 'replaced scanner with', 'replaced damaged scanner', 'replaced scanner and synced to station', 'replaced scanner with asset', 'scanner replaced', 'replaced missing scanner', 'replaced scanner at station', 'replaced broken scanner', 'replaced defective scanner', 'replaced defective scanner with new'],
    1:['station has been rebooted', 'rebooted station', 'performed a remote reboot of the ar station', 'rebooted station remotely', 'remotely reset station', 'restarted station', 'station has been rebooted resolving ticket', 'reboot station verified station is fully operational', 'station rebooted verified that the station is back online', 'rebooted station operational', 'remotely rebooted station', 'reboot of station', 'station has been remotely rebooted', 'station rebooted', 'rebooted the station', 'station was rebooted', 'remotely rebooted the station', 'station was successfully rebooted resolving ticket', 'station has been rebooted verified that the station is back online', 'rebooted the station and all is well', 'station rebooted remotely', 'ar station remotely rebooted', 'rebooted station  issue resolved', 'station rebooted and verified up', 'after rebooting station works good', 'rebooted station issue resolved', 'station rebooted successfully no further issues reported', 'station remotely rebooted', 'successfully rebooted the station as requested', 'rebooted station and tested', 'reboot station', 'sshd into station and reboot', 'station rebooted verified station functionality issue resolved'],
    2:['password reset', 'password has been reset', 'reset password', 'reset users password', 'password rotated', 'peap password rotated', 'the password has been changed', 'reset password for associate', 'i assisted the user with a password reset', 'reset password resolving this tt', 'assisted with password reset at it hub kiosk', 'password successfully reset', 'password rotated on the peap portal', 'password rotation is completed', 'successfully reset password', 'password reset successfully', 'reset user password', 'verified user assisted requester with password reset', 'password was reset', 'password changed', 'assisted with password reset', 'reset password for user', 'password reseted', 'password reset done', 'reset the pwd using the password tool hence resolving this tt', 'the password was updated', 'helped aa to reset their password via password tool with admin rights', 'assisted associate with password reset', 'sopno password has been reset', 'reset password for aa', 'password has been reset successfully', 'performed an inperson password reset via password tool', 'password reset for user'],
    3:['replaced printer', 'replaced the printer', 'printer replaced', 'printer has been replaced', 'printer was replaced', 'replaced printer and tested'],
    4:['rebooted thin client', 'tc has been remotely rebooted', 'rebooted the thin client', 'rebooted tc', 'rebooted thinclient', 'tc have been rebooted functionality has been reestablished'],
    5:['printer reconfigured', 'reconfigured printer with zebra tool successfully for ib destinations', 'reconfigured printer', 'printer configured', 'recalibrated printer', 'reconfigured the printer', 'recalibrated the printer', 'calibrated printer', 'configured printertested its working now', 'printer reconfigured its working now', 'printer has been reconfigured', 'configured and tested printer', 'printer has been configured', 'configured printer', 'pushed correct configuration to printer verified everything works resolving'],
    6:['laptop returned to it', 'laptop has been returned', 'laptop returned', 'the laptop has been returned', 'loaner laptop has been returned', 'loaned the user a laptop and made sure it returned to it', 'laptop returned closing', 'laptop replaced', 'laptop was returned', 'loaner laptop received from user', 'laptop returned closing the ticket'],
    7:['replaced keyboard', 'keyboard replaced', 'keyboard has been replaced', 'replaced the keyboard', 'keyboard was replaced', 'replaced defective keyboard'],
    8:['replaced scanner cable', 'scanner cable replaced', 'replaced the scanner cable', 'scanner cable has been replaced'],
    9:['replaced thin client', 'thin client replaced', 'replaced the thin client', 'thinclient replaced', 'tc replaced'],
    10:['scanner reconfigured', 'reconfigured scanner', 'scanner was reconfigured', 'reconfigured the scanner', 'scanner configured'],
    11:['replaced monitor', 'monitor replaced'],
    12:['reinstalled printer and drivers'],
    13:['replaced mouse', 'mouse replaced', 'mouse has been replaced', 'mouse was replaced', 'replaced the mouse', 'replaced defective mouse'],
    14:['restarted thin client', 'thin client rebooted', 'restarted the thin client', 'reboot thin client', 'rebooted the thinclient', 'performed hard reboot of thin client', 'thinclient rebooted'],
    15:['deployed scanner','deployed scanner to station', 'scanner deployed', 'deployed new scanner', 'deployed a scanner', 'scanners deployed', 'deployed new scanner to station'],
    16:['pslip cable was unplugged reconnected pslip cable then ran test print to verify that issue is resolved'],
    17:['cable replaced', 'replaced cable', 'cable has been replaced', 'replaced the cable'],
    18:['replaced battery', 'replaced the battery', 'battery replaced'],
    19:['unlocked account'],
    20:['reimaged laptop', 'reimaged the laptop', 'laptop reimaged'],
    21:['rollbacked mcm root cause should be found in master tt'],
    22:['reassigned ports and tested', 'issue resolved printer port reassigned', 'printer port was reassigned', 'reassigned printer ports verified slim', 'reassigned printer port'],
    23:['replaced laptop'],
    24:['resynced scanner to base'],
    25:['camera removed from proxemics', 'removed cameras', 'cameras removed as requested', 'cameras removed from proxemics', 'camera removed as requested', 'camera has been removed from proxemics', 'cameras have been removed from proxemics'],
    26:['the account has been unlocked'],
    27:['replaced screen','screen replaced'],
    28:['confirmed images are uploading correctly to'],
    29:['replaced laptop screen'],
    30:['replaced print head'],
    31:['monitor has been replaced','replaced the monitor'],
    32:['rebooted server per cm resolving to see if any alerts refire'],
    33:['replaced usb cable','usb cable replaced'],
    34:['reconnected usb cable','usb cable was disconnected connected back tested working ok now resolving'],
    35:['replaced power cable'],
    36:['wifi card replaced'],
    37:['reassigned printer ports','printer port reassigned issue resolved','reassigned printer ports slim updated'],
    38:['advised to reach out to global it'],
    39:['replaced pslip printer'],
    40:['moved cameras to server'],
    41:['restarted print spooler'],
    42:['replaced hand scanner'],
    43:['resynced scanner to station'],
    44:['upgraded ios version on switch verified all connections to uplinks are restored'],
    45:['printer installed', 'printer deployed'],
    46:['xterm replaced']
}
d = {'resolution' : ['replaced scanner', 'replaced the scanner for the user with a properly working one from the cage replaced the wire on the damaged one and stored it for later use', 'tc reimage', 'updated pc', 'deploying replacement scanner', 'upgraded and rebooted station', 'printer has been reconfigured', 'cleared linux print queue and now it is working','user reset her password successfully closing tt','have reset the printer to get it to print again','i plugged usb cable into port and scanner works','reconfigured hand scanner and linked to station','replaced the scanner with station is functional','laptops battery needed to be reset asset serial','reconfigured scanner confirmed that it scans as intended','reimaging laptop corrected the anyconnect software issue','printer was unplugged from usb port working properly now','reconnected usb cable and reassign printer ports on port','reconfigured scanner to base and tested with aa all fine','replaced the defective device with a fresh imaged laptop','reconfigured the printer and the media to print properly','tested printer at station connected and working resolved','red scanner reconfigured and base rebooted via usb joint','station scanner was synced to base and station and is now working','printer offlineswitched usb portprinter is now online and working','replaced the barcode label with one reflecting the tcs ip address','restarted the thin client by using ssh to run the restart command','printer reconfigured and test they are functioning normally again','removed old printer for service installed replacement tested good','tc required reboot rebooted tc had aa signin dp is now functional','resetting the printer to factory settings and then reconfigure it','updated windows os forced update and the laptop operated normally','printer settings are set correct and printer is working correctly','power to printer was disconnected reconnected and is working fine','power cycled equipment and restocked spooler with plastic bubbles','laptop checked ive logged into paskiplacowepl without any problem','reseated scanner cables connection into usb port to resolve issue','the scanner has been replaced and the station is working well now']}

df_sample = pd.DataFrame(data=d)
我有一个像这样的数据框

labels = {
    0:['replaced scanner', 'scanner has been replaced', 'replaced the scanner', 'scanner was replaced', 'replaced scanner and tested', 'i replaced the scanner', 'deployed replacement scanner', 'replaced scanner with a new one', 'replaced scanner with', 'replaced damaged scanner', 'replaced scanner and synced to station', 'replaced scanner with asset', 'scanner replaced', 'replaced missing scanner', 'replaced scanner at station', 'replaced broken scanner', 'replaced defective scanner', 'replaced defective scanner with new'],
    1:['station has been rebooted', 'rebooted station', 'performed a remote reboot of the ar station', 'rebooted station remotely', 'remotely reset station', 'restarted station', 'station has been rebooted resolving ticket', 'reboot station verified station is fully operational', 'station rebooted verified that the station is back online', 'rebooted station operational', 'remotely rebooted station', 'reboot of station', 'station has been remotely rebooted', 'station rebooted', 'rebooted the station', 'station was rebooted', 'remotely rebooted the station', 'station was successfully rebooted resolving ticket', 'station has been rebooted verified that the station is back online', 'rebooted the station and all is well', 'station rebooted remotely', 'ar station remotely rebooted', 'rebooted station  issue resolved', 'station rebooted and verified up', 'after rebooting station works good', 'rebooted station issue resolved', 'station rebooted successfully no further issues reported', 'station remotely rebooted', 'successfully rebooted the station as requested', 'rebooted station and tested', 'reboot station', 'sshd into station and reboot', 'station rebooted verified station functionality issue resolved'],
    2:['password reset', 'password has been reset', 'reset password', 'reset users password', 'password rotated', 'peap password rotated', 'the password has been changed', 'reset password for associate', 'i assisted the user with a password reset', 'reset password resolving this tt', 'assisted with password reset at it hub kiosk', 'password successfully reset', 'password rotated on the peap portal', 'password rotation is completed', 'successfully reset password', 'password reset successfully', 'reset user password', 'verified user assisted requester with password reset', 'password was reset', 'password changed', 'assisted with password reset', 'reset password for user', 'password reseted', 'password reset done', 'reset the pwd using the password tool hence resolving this tt', 'the password was updated', 'helped aa to reset their password via password tool with admin rights', 'assisted associate with password reset', 'sopno password has been reset', 'reset password for aa', 'password has been reset successfully', 'performed an inperson password reset via password tool', 'password reset for user'],
    3:['replaced printer', 'replaced the printer', 'printer replaced', 'printer has been replaced', 'printer was replaced', 'replaced printer and tested'],
    4:['rebooted thin client', 'tc has been remotely rebooted', 'rebooted the thin client', 'rebooted tc', 'rebooted thinclient', 'tc have been rebooted functionality has been reestablished'],
    5:['printer reconfigured', 'reconfigured printer with zebra tool successfully for ib destinations', 'reconfigured printer', 'printer configured', 'recalibrated printer', 'reconfigured the printer', 'recalibrated the printer', 'calibrated printer', 'configured printertested its working now', 'printer reconfigured its working now', 'printer has been reconfigured', 'configured and tested printer', 'printer has been configured', 'configured printer', 'pushed correct configuration to printer verified everything works resolving'],
    6:['laptop returned to it', 'laptop has been returned', 'laptop returned', 'the laptop has been returned', 'loaner laptop has been returned', 'loaned the user a laptop and made sure it returned to it', 'laptop returned closing', 'laptop replaced', 'laptop was returned', 'loaner laptop received from user', 'laptop returned closing the ticket'],
    7:['replaced keyboard', 'keyboard replaced', 'keyboard has been replaced', 'replaced the keyboard', 'keyboard was replaced', 'replaced defective keyboard'],
    8:['replaced scanner cable', 'scanner cable replaced', 'replaced the scanner cable', 'scanner cable has been replaced'],
    9:['replaced thin client', 'thin client replaced', 'replaced the thin client', 'thinclient replaced', 'tc replaced'],
    10:['scanner reconfigured', 'reconfigured scanner', 'scanner was reconfigured', 'reconfigured the scanner', 'scanner configured'],
    11:['replaced monitor', 'monitor replaced'],
    12:['reinstalled printer and drivers'],
    13:['replaced mouse', 'mouse replaced', 'mouse has been replaced', 'mouse was replaced', 'replaced the mouse', 'replaced defective mouse'],
    14:['restarted thin client', 'thin client rebooted', 'restarted the thin client', 'reboot thin client', 'rebooted the thinclient', 'performed hard reboot of thin client', 'thinclient rebooted'],
    15:['deployed scanner','deployed scanner to station', 'scanner deployed', 'deployed new scanner', 'deployed a scanner', 'scanners deployed', 'deployed new scanner to station'],
    16:['pslip cable was unplugged reconnected pslip cable then ran test print to verify that issue is resolved'],
    17:['cable replaced', 'replaced cable', 'cable has been replaced', 'replaced the cable'],
    18:['replaced battery', 'replaced the battery', 'battery replaced'],
    19:['unlocked account'],
    20:['reimaged laptop', 'reimaged the laptop', 'laptop reimaged'],
    21:['rollbacked mcm root cause should be found in master tt'],
    22:['reassigned ports and tested', 'issue resolved printer port reassigned', 'printer port was reassigned', 'reassigned printer ports verified slim', 'reassigned printer port'],
    23:['replaced laptop'],
    24:['resynced scanner to base'],
    25:['camera removed from proxemics', 'removed cameras', 'cameras removed as requested', 'cameras removed from proxemics', 'camera removed as requested', 'camera has been removed from proxemics', 'cameras have been removed from proxemics'],
    26:['the account has been unlocked'],
    27:['replaced screen','screen replaced'],
    28:['confirmed images are uploading correctly to'],
    29:['replaced laptop screen'],
    30:['replaced print head'],
    31:['monitor has been replaced','replaced the monitor'],
    32:['rebooted server per cm resolving to see if any alerts refire'],
    33:['replaced usb cable','usb cable replaced'],
    34:['reconnected usb cable','usb cable was disconnected connected back tested working ok now resolving'],
    35:['replaced power cable'],
    36:['wifi card replaced'],
    37:['reassigned printer ports','printer port reassigned issue resolved','reassigned printer ports slim updated'],
    38:['advised to reach out to global it'],
    39:['replaced pslip printer'],
    40:['moved cameras to server'],
    41:['restarted print spooler'],
    42:['replaced hand scanner'],
    43:['resynced scanner to station'],
    44:['upgraded ios version on switch verified all connections to uplinks are restored'],
    45:['printer installed', 'printer deployed'],
    46:['xterm replaced']
}
d = {'resolution' : ['replaced scanner', 'replaced the scanner for the user with a properly working one from the cage replaced the wire on the damaged one and stored it for later use', 'tc reimage', 'updated pc', 'deploying replacement scanner', 'upgraded and rebooted station', 'printer has been reconfigured', 'cleared linux print queue and now it is working','user reset her password successfully closing tt','have reset the printer to get it to print again','i plugged usb cable into port and scanner works','reconfigured hand scanner and linked to station','replaced the scanner with station is functional','laptops battery needed to be reset asset serial','reconfigured scanner confirmed that it scans as intended','reimaging laptop corrected the anyconnect software issue','printer was unplugged from usb port working properly now','reconnected usb cable and reassign printer ports on port','reconfigured scanner to base and tested with aa all fine','replaced the defective device with a fresh imaged laptop','reconfigured the printer and the media to print properly','tested printer at station connected and working resolved','red scanner reconfigured and base rebooted via usb joint','station scanner was synced to base and station and is now working','printer offlineswitched usb portprinter is now online and working','replaced the barcode label with one reflecting the tcs ip address','restarted the thin client by using ssh to run the restart command','printer reconfigured and test they are functioning normally again','removed old printer for service installed replacement tested good','tc required reboot rebooted tc had aa signin dp is now functional','resetting the printer to factory settings and then reconfigure it','updated windows os forced update and the laptop operated normally','printer settings are set correct and printer is working correctly','power to printer was disconnected reconnected and is working fine','power cycled equipment and restocked spooler with plastic bubbles','laptop checked ive logged into paskiplacowepl without any problem','reseated scanner cables connection into usb port to resolve issue','the scanner has been replaced and the station is working well now']}

df_sample = pd.DataFrame(data=d)

我想将字典中的
应用于数据帧。换句话说,我想在
df\u sample
中创建一个名为
cluster\u no
的新列。如果字典中有
resolution
,则我想获取相关的密钥。我希望这是有道理的。非常感谢您的任何建议或帮助。

只需创建一个反向字典和地图:

inv_labels={x:k for k,v in labels.items() for x in v}

df_sample['cluster_no'] = df_sample['resolution'].map(inv_labels)
输出:

                                           resolution  cluster_no
0                                    replaced scanner         0.0
1   replaced the scanner for the user with a prope...         NaN
2                                          tc reimage         NaN
3                                          updated pc         NaN
4                       deploying replacement scanner         NaN
5                       upgraded and rebooted station         NaN
6                       printer has been reconfigured         5.0
7     cleared linux print queue and now it is working         NaN
8     user reset her password successfully closing tt         NaN
9     have reset the printer to get it to print again         NaN
....

只需创建一个反向字典和映射:

inv_labels={x:k for k,v in labels.items() for x in v}

df_sample['cluster_no'] = df_sample['resolution'].map(inv_labels)
输出:

                                           resolution  cluster_no
0                                    replaced scanner         0.0
1   replaced the scanner for the user with a prope...         NaN
2                                          tc reimage         NaN
3                                          updated pc         NaN
4                       deploying replacement scanner         NaN
5                       upgraded and rebooted station         NaN
6                       printer has been reconfigured         5.0
7     cleared linux print queue and now it is working         NaN
8     user reset her password successfully closing tt         NaN
9     have reset the printer to get it to print again         NaN
....