简体   繁体   English

Python Pandas 循环通过 Dataframe 无法正常工作

[英]Python Pandas looping through Dataframe not working properly

Trying to write a pandas/python program to do a api call and extract status of hosts and iterate through one of the column of data frame and add status column to the output尝试编写一个 pandas/python 程序来执行 api 调用并提取主机状态并遍历数据框列之一并将状态列添加到 output

What I am trying:我正在尝试什么:

import requests
import json
import pandas as pd

df = pd.read_table('c:\csv\input1.csv', engine='python', sep="\s*,\s*", skipinitialspace=True, usecols=[0, 1])
for v in df['Private Ip']:

    headers = {
        'X-Requested-By': 'cli',
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    }
    params = (
        ('query', v),
        ('range', '86400'),
        ('limit', '100'),
        ('sort', 'timestamp:desc'),
        ('pretty', 'true'),
    )
    response = requests.get('http://xxx.xx.xxx.xxx:9000/api/search/universal/relative', headers=headers, params=params,
                            auth=('xxxxxxx', 'xxxxxxxx'))

    text = response.text    

    pretty_json = json.loads(text)
    col1 = df['Account Name'] = df['Account Name'].astype(str)
    col2 = df['Private Ip'] = df['Private Ip'].astype(str)

    if pretty_json.get('messages'):
        print(col1 + ',' + col2 + ',' + 'Yes')

    else:
      print(col1 + ',' + col2 + ',' + 'No')

Inputs:输入:

My CSV:

Account Name,Hosts 
ABCD,XX.XXX.XX.XX           
ABCDE,XX.XX.XXX.XX
ABCDEF,XX.XX.XXX.XXX
ABCDEFG,XX.XXX.XX.XX

The problem: The loop is going 4 times (actually It should run for just 4 times as i am trying to loop row by row, that means It need to run 4 times问题:循环进行了 4 次(实际上它应该只运行 4 次,因为我试图逐行循环,这意味着它需要运行 4 次

output i am getting: ( Its looping for 4 times, i guess pandas is considering entire df set as one iteration not an indidual row of data frame) output 我得到:(它循环了 4 次,我猜 pandas 正在考虑将整个 df 集视为一次迭代,而不是单独的数据帧行)

0 ABCD,XX.XXX.XX.XX,Yes         
1 ABCDE,XX.XX.XXX.XX,Yes
2 ABCDEF,XX.XX.XXX.XXX,Yes
3 ABCDEFG,XX.XXX.XX.XX,Yes
dtype: object

0 ABCD,XX.XXX.XX.XX,Yes         
1 ABCDE,XX.XX.XXX.XX,Yes
2 ABCDEF,XX.XX.XXX.XXX,Yes
3 ABCDEFG,XX.XXX.XX.XX,Yes
dtype: object

0 ABCD,XX.XXX.XX.XX,Yes         
1 ABCDE,XX.XX.XXX.XX,Yes
2 ABCDEF,XX.XX.XXX.XXX,Yes
3 ABCDEFG,XX.XXX.XX.XX,Yes
dtype: object

0 ABCD,XX.XXX.XX.XX,Yes         
1 ABCDE,XX.XX.XXX.XX,Yes
2 ABCDEF,XX.XX.XXX.XXX,Yes
3 ABCDEFG,XX.XXX.XX.XX,Yes
dtype: object

(the below is wrong as all hostgot has messages in json returned, not sure why its below else logic is getting executed)
    0 ABCD,XX.XXX.XX.XX,No          
    1 ABCDE,XX.XX.XXX.XX,No
    2 ABCDEF,XX.XX.XXX.XXX,No
    3 ABCDEFG,XX.XXX.XX.XX,No
    dtype: object

can some one suggest.有人可以建议。 I guess im missing looping indenting somewhere, not sure where..Please suggest我想我错过了在某处循环缩进,不确定在哪里..请建议

I believe the problem are those lines -我相信问题是那些线 -

col1 = df['Account Name'] = df['Account Name'].astype(str)
col2 = df['Private Ip'] = df['Private Ip'].astype(str)

It does multiple assignment from right to left and refers to the entire datagram.它从右到左进行多重赋值,并引用整个数据报。 Consider the following code -考虑以下代码 -

 a = 3
 b = 4
 c = a = b

After this code is executed, a,b,c are all equal to 4.执行此代码后, a,b,c都等于 4。

I suggest using pandas apply method to iterate over the rows.我建议使用 pandas 应用方法来迭代行。

Fixed the issued with修复了发布的问题

for index, row in df.iterrows():对于索引,df.iterrows() 中的行:

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

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