Python Pandas在csv行上应用函数时循环两次

Python Pandas在csv行上应用函数时循环两次,python,csv,pandas,Python,Csv,Pandas,我有一个csv文档,其中一列名为“IP地址”,另外三列中有随机数据。我的目标是在IP地址列中循环,并在每个IP地址上运行bulkfunc,将内容输出到文本文件 我让panda正确地访问数据,但由于某些原因,它循环了两次,因此,由于CSV中有3个IP地址,我得到了6个输出文件 def bulkcsv(): df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address']) #for row in df

我有一个csv文档,其中一列名为“IP地址”,另外三列中有随机数据。我的目标是在IP地址列中循环,并在每个IP地址上运行bulkfunc,将内容输出到文本文件

我让panda正确地访问数据,但由于某些原因,它循环了两次,因此,由于CSV中有3个IP地址,我得到了6个输出文件

def bulkcsv():
  df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address'])
  #for row in df:
  df.applymap(bulkfunc)
以下是我的bulkfunc函数:

def bulkfunc(x):
  global f
  global ip
  ip = x
  f = open('results/%s' % ip + "_" + datetime.now().strftime("%Y-%m-%d@%H%M") + '.txt', 'a')
  print "Static Information: "
  f.write("Static Information: ")
  print "-" * 30
  f.write("-" * 30)
  parsenetworkcsv(ip)
  ping(ip)
  nmaprun(ip, "-sV")
  print "The output is complete."
  f.write("-" * 30)
  f.write("created by OP")
  f.close()
以下是csv:

IP Address  random crap   hiya    intwer
10.90.11.252    dawd4     4654    14512
10.90.11.253    144         0
10.90.11.254    203 

试试这个,这个应该可以解决问题

import pandas as pd
import time
from datetime import datetime

def bulkcsv():
  inputfile = 'inp.csv'
  df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address'])
  #print df
  #for row in df:
  #print df['IP Address'].shape
  df['IP Address'].map(bulkfunc)

def bulkfunc(x):
  global f
  global ip
  ip = x
  f = open('results/%s' % ip + "_" + datetime.now().strftime("%Y-%m-%d@%H%M%S") + '.txt', 'a')
  print "Static Information: "
  f.write("Static Information: ")
  print "-" * 30
  f.write("-" * 30)
  parsenetworkcsv(ip)
  ping(ip)
  nmaprun(ip, "-sV")
  print "The output is complete."
  f.write("-" * 30)
  f.write("created by OP")
  f.close()
  time.sleep(1)

bulkcsv()
输出:

     IP Address
0  10.90.11.252
1  10.90.11.253
2  10.90.11.254
(3L,)
Static Information:
------------------------------
The output is complete.
Static Information:
------------------------------
The output is complete.
Static Information:
------------------------------
The output is complete.
问题似乎是因为pd.read\u csv。在代码中,您已将其作为数据帧读取。它的形状是(3L,1),因此applymap循环两次(索引0和1)。但是,当我们作为一个系列使用时,因为您只有一列,所以映射为您完成了这项工作。您还可以使用、应用DataFrame的函数。我相信applymap对于超过1维的数据帧工作得很好,否则它应该被视为一个系列


我相信这可能是一个错误或改变熊猫的要求。你可以试试那条路线。

试试这个,这应该可以解决问题

import pandas as pd
import time
from datetime import datetime

def bulkcsv():
  inputfile = 'inp.csv'
  df = pd.read_csv(('csvfiles/' + inputfile), dtype=str, usecols=['IP Address'])
  #print df
  #for row in df:
  #print df['IP Address'].shape
  df['IP Address'].map(bulkfunc)

def bulkfunc(x):
  global f
  global ip
  ip = x
  f = open('results/%s' % ip + "_" + datetime.now().strftime("%Y-%m-%d@%H%M%S") + '.txt', 'a')
  print "Static Information: "
  f.write("Static Information: ")
  print "-" * 30
  f.write("-" * 30)
  parsenetworkcsv(ip)
  ping(ip)
  nmaprun(ip, "-sV")
  print "The output is complete."
  f.write("-" * 30)
  f.write("created by OP")
  f.close()
  time.sleep(1)

bulkcsv()
输出:

     IP Address
0  10.90.11.252
1  10.90.11.253
2  10.90.11.254
(3L,)
Static Information:
------------------------------
The output is complete.
Static Information:
------------------------------
The output is complete.
Static Information:
------------------------------
The output is complete.
问题似乎是因为pd.read\u csv。在代码中,您已将其作为数据帧读取。它的形状是(3L,1),因此applymap循环两次(索引0和1)。但是,当我们作为一个系列使用时,因为您只有一列,所以映射为您完成了这项工作。您还可以使用、应用DataFrame的函数。我相信applymap对于超过1维的数据帧工作得很好,否则它应该被视为一个系列


我相信这可能是一个错误或改变熊猫的要求。你可以试试那条路线。

首先,我认为你不需要for循环,df.applymap应该足够了。请参阅位于的熊猫文档。看看这是否解决了问题。另外,为了提出解决方案,我们非常感谢您的输入数据示例。我去掉了循环,但它仍然循环了两次。但是感谢您的建议,我首先添加了它们,我认为您不需要for循环,df.applymap应该足够了。请参阅位于的熊猫文档。看看这是否解决了问题。另外,为了提出解决方案,我们非常感谢您的输入数据示例。我去掉了循环,但它仍然循环了两次。但是感谢您的建议,我添加了它们谢谢您的帮助!非常感谢!谢谢你的帮助!非常感谢!