我是一名新手,对Python和Pandas不太了解,过去几周一直在寻找答案,但却遇到了难题。虽然我通常可以在这里或者网络上找到答案,但这次却没有那么幸运。
问题:
我有一个Excel文件,需要循环读取其中的内容来为防火墙创建一个脚本。 第1、2、3行的A列已经合并,包含一个主机名; 第1、2、3行的B列没有合并,每一行分别包含一个IP地址。
如何编写条件语句,以判断如果A列有任何合并的行,则检查并合并B列中相同的行?
我的代码:
问题:
我有一个Excel文件,需要循环读取其中的内容来为防火墙创建一个脚本。 第1、2、3行的A列已经合并,包含一个主机名; 第1、2、3行的B列没有合并,每一行分别包含一个IP地址。
如何编写条件语句,以判断如果A列有任何合并的行,则检查并合并B列中相同的行?
我的代码:
import pandas as pd
df = pd.read_excel('file.xlsx',sheetname="sheet_name")
df1 = df['Unnamed: 2'].dropna().drop([6,7]) # Unanamed is the column ID then Delete empty cell's and delete row 6&7
df2 = df['Unnamed: 4'].dropna().drop([6,7]) # Unanamed is the column ID then Delete empty cell's and delete row 6&7
for a, b in zip(df1,df2):
# The condition I am looking for, please note the code below is not correct
if a is merged then merge b:
print('Hostname {} ip address {}'.format(a,b))
提前致谢。