在multyindex列上合并pandas数据帧

时间:2018-03-05 14:17:12

标签: python pandas merge

我想在我的数据帧上应用一个函数,添加有关每个id的平均性能的信息。问题是使用apply应用此函数需要花费很多时间(我在20分钟后停止了该过程)并且我需要做几次。所以我想尝试将信息取出到一个新的数据框,然后将两者合并在一起。问题是我的新数据框有multyindex并且我试图合并该索引。似乎无法找到实现目标的方法。有什么建议吗?

新数据框如下所示:

                              compliance_detail
    inspector_name  compliance  
    Addison, Michael    0.0 0.431203
                        1.0 0.034050
    Ahmad, Muna I       0.0 0.731405
                        1.0 0.052342
    Anderson, Trevis    0.0 0.166667

我希望合并检查员的姓名和合规性,因为我在原始数据框中有这两个。

我希望合并后的数据框能为每个检查员提供他对于抱怨== 0和抱怨== 1的评级。所以它看起来像这样:

    ticket_id   inspector_name  positive_complaince negetive_complaince
    0   22056   Addison, Michael 0.034050            0.431203
    1   22057   Ahmad, Muna I    0.052342            0.731405

2 个答案:

答案 0 :(得分:0)

您可以使用<? xml version="1.0" encoding="utf-8"?> <unattend xmlns = "urn:schemas-microsoft-com:unattend" > < settings pass="windowsPE"> <component name = "Microsoft-Windows-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"> <ImageInstall> <OSImage> <WillShowUI>OnError</WillShowUI> <InstallTo> <DiskID>0</DiskID> <PartitionID>1</PartitionID> </InstallTo> <InstallFrom> <Path>.\Operating Systems\os\os.wim</Path> <MetaData> <Key>/IMAGE/INDEX</Key> <Value>1</Value> </MetaData> </InstallFrom> <InstallToAvailablePartition>true</InstallToAvailablePartition> </OSImage> </ImageInstall> <ComplianceCheck> <DisplayReport>OnError</DisplayReport> </ComplianceCheck> <UserData> <AcceptEula>true</AcceptEula> <ProductKey> <Key>WindowsSerialKey</Key> <WillShowUI>Never</WillShowUI> </ProductKey> <Organization>Stuiter</Organization> </UserData> <EnableFirewall>true</EnableFirewall> <EnableNetwork>true</EnableNetwork> <Restart>Restart</Restart> </component> <component name = "Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <SetupUILanguage> <UILanguage>en-US</UILanguage> </SetupUILanguage> <InputLocale>0409:00000409</InputLocale> <SystemLocale>en-US</SystemLocale> <UILanguage>en-US</UILanguage> <UserLocale>en-US</UserLocale> </component> </settings> <settings pass = "generalize" > < component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DoNotCleanTaskBar>true</DoNotCleanTaskBar> </component> </settings> <settings pass = "oobeSystem" > < component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"> <UserAccounts> <AdministratorPassword> <Value>hashed password</Value> <PlainText>false</PlainText> </AdministratorPassword> <LocalAccounts> <LocalAccount wcm:action= "add" > < Password > < Value > hashed password</Value> <PlainText>false</PlainText> </Password> <Group>Users</Group> <Description>User</Description> <DisplayName>User account</DisplayName> <Name>User</Name> </LocalAccount> <LocalAccount wcm:action= "add" > < Password > < Value > hashed password</Value> <PlainText>false</PlainText> </Password> <Description>Admin user</Description> <DisplayName>Admin user account</DisplayName> <Name>AdminUser</Name> <Group>Administrators</Group> </LocalAccount> <LocalAccount wcm:action= "add" > < Password > < Value > hashed password</Value> <PlainText>false</PlainText> </Password> <Description>User for Support</Description> <DisplayName>Support user account</DisplayName> <Group>Users</Group> <Name>Support</Name> </LocalAccount> </LocalAccounts> </UserAccounts> <AutoLogon> <Enabled>true</Enabled> <Username>Rimote</Username> <Domain>.</Domain> <Password> <Value>hashed password</Value> <PlainText>false</PlainText> </Password> <LogonCount>2147483647</LogonCount> </AutoLogon> <Display> <ColorDepth>32</ColorDepth> <HorizontalResolution>1</HorizontalResolution> <RefreshRate>60</RefreshRate> <VerticalResolution>1</VerticalResolution> <DPI>120</DPI> </Display> <FirstLogonCommands> <SynchronousCommand wcm:action= "add" > < CommandLine > wscript.exe % SystemDrive %\LTIBootstrap.vbs</CommandLine> <Description>Lite Touch new OS</Description> <Order>1</Order> </SynchronousCommand> </FirstLogonCommands> <OOBE> <HideEULAPage>false</HideEULAPage> <ProtectYourPC>2</ProtectYourPC> <HideLocalAccountScreen>false</HideLocalAccountScreen> <HideOnlineAccountScreens>false</HideOnlineAccountScreens> <HideWirelessSetupInOOBE>false</HideWirelessSetupInOOBE> <HideOEMRegistrationScreen>false</HideOEMRegistrationScreen> <UnattendEnableRetailDemo>false</UnattendEnableRetailDemo> </OOBE> <RegisteredOrganization>Stuiter</RegisteredOrganization> <RegisteredOwner>Stuiter</RegisteredOwner> <TimeZone>W.Europe Standard Time</TimeZone> <BluetoothTaskbarIconEnabled>false</BluetoothTaskbarIconEnabled> <ConvertibleSlateModePromptPreference>1</ConvertibleSlateModePromptPreference> <DisableAutoDaylightTimeSet>false</DisableAutoDaylightTimeSet> <DoNotCleanTaskBar>false</DoNotCleanTaskBar> <EnableStartMenu>false</EnableStartMenu> <ShowPowerButtonOnStartScreen>false</ShowPowerButtonOnStartScreen> <SignInMode>1</SignInMode> <WindowsFeatures> <ShowInternetExplorer>false</ShowInternetExplorer> <ShowMediaCenter>false</ShowMediaCenter> <ShowWindowsMediaPlayer>false</ShowWindowsMediaPlayer> </WindowsFeatures> </component> <component name = "Microsoft-Windows-International-Core" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <InputLocale>0409:00000409</InputLocale> <SystemLocale>en-US</SystemLocale> <UILanguage>en-US</UILanguage> <UserLocale>en-US</UserLocale> </component> </settings> <settings pass = "offlineServicing" > < component name="Microsoft-Windows-PnpCustomizationsNonWinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DriverPaths> <PathAndCredentials wcm:keyValue="1" wcm:action="add"> <Path>\Drivers</Path> </PathAndCredentials> </DriverPaths> </component> <component name = "Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <DesktopOptimization> <GoToDesktopOnSignIn>true</GoToDesktopOnSignIn> <ShowWindowsStoreAppsOnTaskbar>false</ShowWindowsStoreAppsOnTaskbar> </DesktopOptimization> <OfflineUserAccounts> <OfflineAdministratorPassword> <Value>hashed password</Value> <PlainText>false</PlainText> </OfflineAdministratorPassword> <OfflineLocalAccounts> <LocalAccount wcm:action= "add" > < Password > < Value > hashed password</Value> <PlainText>false</PlainText> </Password> <Description>User</Description> <DisplayName>User</DisplayName> <Group>Users</Group> <Name>User</Name> </LocalAccount> <LocalAccount wcm:action= "add" > < Password > < Value > hashed password</Value> <PlainText>false</PlainText> </Password> <Description>Support user</Description> <DisplayName>Support</DisplayName> <Name>Support</Name> <Group>Users</Group> </LocalAccount> <LocalAccount wcm:action= "add" > < Password > < Value > hashed password</Value> <PlainText>false</PlainText> </Password> <Description>Admin user</Description> <DisplayName>Admin user</DisplayName> <Group>Administrators</Group> <Name>AdminUser</Name> </LocalAccount> </OfflineLocalAccounts> </OfflineUserAccounts> </component> </settings> </unattend> 重塑数据框。此外,根据您的给定输出,我假设您要删除其中一个合规性(正面或负面)不可用的unstack

inspector_name

答案 1 :(得分:0)

好的,我找到了答案。不确定它是否是最好的方式,但它的工作原理。 首先,我采用新的数据框并将其分成两个数据框,一个是合规性== 0,另一个是合规性== 1。然后我将每个数据帧的索引转换为一个列表并将其保存在参数中。现在我采用我的基本数据框并将其与我创建的两个新数据框合并。 merge函数中的“right_on”参数现在等于两个数据帧一侧保存的列表的名称。它的工作原理:) 希望这可以帮助别人。