简体   繁体   English

pandas dataframe 打印重复值

[英]pandas dataframe print repeats values

I have the following source CSV我有以下来源 CSV

"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 ",

In my python script I've trying to print the 4th cell value of every row (ie the process name).在我的 python 脚本中,我试图打印每行的第 4 个单元格值(即进程名称)。 the following function prints it fine but it repeats it self for like 3 times.. What am i doing wrong?以下 function 打印得很好,但它自己重复了 3 次。我做错了什么?

dfProcs = pd.read_csv( args.path + '/windows.pstree.PsTree.csv') dfProcs = pd.read_csv(args.path + '/windows.pstree.PsTree.csv')

for ImageFileName in dfProcs:
    print(dfProcs[ImageFileName].values[0])

Get a list of all the Image File Names from csv data.从 csv 数据中获取所有图像文件名的列表。

Your for loop is iterating over the column names and it just seems that you are just printing out the column value for the first row.您的 for 循环正在遍历列名,看起来您只是打印出第一行的列值。

Instead it seems like you want to iterate over the fourth column and print every value.相反,您似乎想要遍历第四列并打印每个值。 This is one way of doing that这是这样做的一种方法

for fileName in dfProcs["ImageFileName"]:
   print(fileName)

I would recommend you to use itterrows() because it's more versatile, and quite often you will use other parameters like just index that itterrows returns (index, Series)我建议你使用itterrows()因为它更通用,而且你经常会使用其他参数,比如 itterrows 返回的索引 (index, Series)

How to use it in your case ->如何在您的情况下使用它->

for index, row in dfProcs.iterrows():
    print(row["ImageFileName"])

return返回

System
Registry
smss.exe
csrss.exe
wininit.exe
services.exe
svchost.exe
svchost.exe

How to use it and theory -> https://towardsdatascience.com/how-to-iterate-over-rows-in-a-panas-dataframe-6aa173fc6c84如何使用它和理论 -> https://towardsdatascience.com/how-to-iterate-over-rows-in-a-panas-dataframe-6aa173fc6c84

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

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