简体   繁体   English

使用现有的 csv 数据在 dataframe 中创建一个新列

[英]Create a new column within dataframe using existing csv data

So I have the following CSV data.所以我有以下 CSV 数据。 If you look at the columns, PPID is the parent process ID and PID is the process ID.如果查看列,PPID 是父进程 ID,PID 是进程 ID。 I want to update my existing dataframe so that i can add a new column called PPIDName with the corresponding name of the process rather than an ID.我想更新我现有的 dataframe 以便我可以添加一个名为 PPIDName 的新列,其中包含相应的进程名称而不是 ID。 How can I go about doing this?我怎么能go做这个呢?

Following is an example:下面是一个例子:

PID of services.exe is 768. PPID of svchost.exe is PPID as 768 (which is services.exe). services.exe的PID为768。svchost.exe的PPID为768(即services.exe)。 I want to make a new column in this so that for every row I print out the actual name of the parent process rather than its PPID我想在此创建一个新列,以便为每一行打印出父进程的实际名称而不是其 PPID

"TreeDepth","PID","PPID","ImageFileName","Offset(V)","Threads","Handles","SessionId","Wow64","CreateTime","ExitTime"
1,768,632,"services.exe","0xac8190e52100",7,,0,False,"2021-04-01 05:05:01.000000 ", 
2,1164,768,"svchost.exe","0xac8191053340",3,,0,False,"2021-04-01 05:05:02.000000 ",
"TreeDepth","PID","PPID","ImageFileName","Offset(V)","Threads","Handles","SessionId","Wow64","CreateTime","ExitTime"
0,4,0,"System","0xac818d45d080",158,,,False,"2021-04-01 05:04:58.000000 ",
1,88,4,"Registry","0xac818d5ab040",4,,,False,"2021-04-01 05:04:54.000000 ",
1,404,4,"smss.exe","0xac818dea7040",2,,,False,"2021-04-01 05:04:58.000000 ",
0,556,548,"csrss.exe","0xac81900e4140",10,,0,False,"2021-04-01 05:05:00.000000 ",
0,632,548,"wininit.exe","0xac81901ee080",1,,0,False,"2021-04-01 05:05:00.000000 ",
1,768,632,"services.exe","0xac8190e52100",7,,0,False,"2021-04-01 05:05:01.000000 ",
2,1152,768,"svchost.exe","0xac8191034300",2,,0,False,"2021-04-01 05:05:02.000000 ",
2,2560,768,"svchost.exe","0xac8191485080",6,,0,False,"2021-04-01 05:05:03.000000 ",
2,1668,768,"svchost.exe","0xac8191238080",6,,0,False,"2021-04-01 05:05:03.000000 ",
2,1924,768,"svchost.exe","0xac819132b340",6,,0,False,"2021-04-01 05:05:03.000000 ",
2,908,768,"svchost.exe","0xac8190076080",1,,0,False,"2021-04-01 05:05:01.000000 ",
2,1164,768,"svchost.exe","0xac8191053340",3,,0,False,"2021-04-01 05:05:02.000000 ",
2,2956,768,"svchost.exe","0xac81915d5080",3,,0,False,"2021-04-01 05:05:04.000000 ",
2,652,768,"svchost.exe","0xac8194af2080",11,,0,False,"2021-04-05 21:59:50.000000 ",
2,1680,768,"svchost.exe","0xac819123a700",9,,0,False,"2021-04-01 05:05:03.000000 ",
2,1172,768,"svchost.exe","0xac8191055380",4,,0,False,"2021-04-01 05:05:02.000000 ",
2,2964,768,"svchost.exe","0xac819163e080",7,,0,False,"2021-04-01 05:05:04.000000 ",
2,4500,768,"svchost.exe","0xac8192760080",4,,0,False,"2021-04-01 05:48:25.000000 ",
2,2196,768,"svchost.exe","0xac8191ff0080",4,,0,False,"2021-04-02 01:20:04.000000 ",
2,2456,768,"svchost.exe","0xac8191333080",6,,0,False,"2021-04-01 05:05:03.000000 ",
2,1688,768,"svchost.exe","0xac819267c2c0",7,,0,False,"2021-04-01 05:48:24.000000 ",
2,1180,768,"svchost.exe","0xac8191058700",4,,0,False,"2021-04-01 05:05:02.000000 ",
2,2588,768,"spoolsv.exe","0xac81914db0c0",15,,0,False,"2021-04-01 05:05:03.000000 ",
2,2716,768,"svchost.exe","0xac8192615340",4,,2,False,"2021-04-01 05:48:24.000000 ",

I think I understand what you're after.我想我明白你在追求什么。

I've made a smaller df with only the relevant columns for my answer (so you can assume Another Col replaces all the other columns):我做了一个较小的 df,其中只有相关的列作为我的答案(所以你可以假设另一个 Col 替换了所有其他列):

     PID  PPID ImageFileName  Another Col
0      4     0        System            1
1     88     4      Registry            2
2    404     4      smss.exe            3
3    556   548     csrss.exe            4
4    632   548   wininit.exe            5
                 ...

Firstly, I got all of the PIDs with their corresponding name, and removed any duplicates (if they exist):首先,我得到了所有具有相应名称的 PID,并删除了所有重复项(如果存在):

df_PID = df[['PID', 'ImageFileName']].drop_duplicates()

     PID ImageFileName
0      4        System
1     88      Registry
2    404      smss.exe
3    556     csrss.exe
4    632   wininit.exe
5    768  services.exe
6   1152   svchost.exe
        ...

I then renamed these columns to PPID and PPIDName, to make it easier to merge onto the original df to get the desired result.然后我将这些列重命名为 PPID 和 PPIDName,以便更容易合并到原始 df 上以获得所需的结果。 That and the merge are below:那和合并如下:

df_PID.columns = ['PPID', 'PPIDName']
df = df.merge(df_PID, on='PPID', how='left')

This gives the below output, which I think is what you want:这给出了下面的 output,我认为这是你想要的:

     PID  PPID ImageFileName  Another Col      PPIDName
0      4     0        System            1           NaN
1     88     4      Registry            2        System
2    404     4      smss.exe            3        System
3    556   548     csrss.exe            4           NaN
4    632   548   wininit.exe            5           NaN
5    768   632  services.exe            6   wininit.exe
6   1152   768   svchost.exe            7  services.exe
7   2560   768   svchost.exe            8  services.exe
8   1668   768   svchost.exe            9  services.exe
9   1924   768   svchost.exe           10  services.exe
                          ...

This does the job,这完成了工作,

ppid_name = df.loc[df["PID"].isin(df["PPID"]), ["PID", "ImageFileName"]].set_index("PID", drop = False)
replace_with = (ppid_name["PID"].astype(str) + "_" + ppid_name["ImageFileName"]).to_dict()
df["PPID"] = df["PPID"].replace(replace_with)

Output - Output -

TreeDepth树深 PID PID PPID PPID ImageFileName图像文件名 Offset(V)偏置(V) Threads线程 Handles把手 SessionId会话Id Wow64哇64 CreateTime创建时间 ExitTime退出时间
0 0 0 0 4 4个 0 0 System系统 0xac818d45d080 0xac818d45d080 158 158 nan nan False错误的 2021-04-01 05:04:58.000000 2021-04-01 05:04:58.000000 nan
1 1个 1 1个 88 88 4_System 4_系统 Registry注册表 0xac818d5ab040 0xac818d5ab040 4 4个 nan nan False错误的 2021-04-01 05:04:54.000000 2021-04-01 05:04:54.000000 nan
2 2个 1 1个 404 404 4_System 4_系统 smss.exe smss.exe 0xac818dea7040 0xac818dea7040 2 2个 nan nan False错误的 2021-04-01 05:04:58.000000 2021-04-01 05:04:58.000000 nan
3 3个 0 0 556 556 548 548 csrss.exe csrss.exe 0xac81900e4140 0xac81900e4140 10 10 nan 0.0 0.0 False错误的 2021-04-01 05:05:00.000000 2021-04-01 05:05:00.000000 nan
4 4个 0 0 632 632 548 548 wininit.exe启动程序 0xac81901ee080 0xac81901ee080 1 1个 nan 0.0 0.0 False错误的 2021-04-01 05:05:00.000000 2021-04-01 05:05:00.000000 nan

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

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