Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在短电子表格中迭代并将匹配值复制到大电子表格中_Python_Pandas - Fatal编程技术网

Python在短电子表格中迭代并将匹配值复制到大电子表格中

Python在短电子表格中迭代并将匹配值复制到大电子表格中,python,pandas,Python,Pandas,我正在与pandas合作,尝试使用设备列表device_master.xlsx自动填充我的主电子表格detailed_billing.xlsx中的许多列 我可以阅读这两张表的内容,也可以对我的主电子表格进行其他转换,但我对这一点还不熟悉,我不知道如何遍历device_master.xlsx表的每一行,并将其与主表中所需的列进行比较/填充 到目前为止,我对工作表其余部分所做的工作如下所示: import numpy as np import pandas as pd import csv #imp

我正在与pandas合作,尝试使用设备列表device_master.xlsx自动填充我的主电子表格detailed_billing.xlsx中的许多列

我可以阅读这两张表的内容,也可以对我的主电子表格进行其他转换,但我对这一点还不熟悉,我不知道如何遍历device_master.xlsx表的每一行,并将其与主表中所需的列进行比较/填充

到目前为止,我对工作表其余部分所做的工作如下所示:

import numpy as np
import pandas as pd
import csv
#import re

# import the list of devices with work order numbers and project codes:

devmaster_xls = pd.read_excel('device_master.xlsx', 'device master', header = [0], index_col = None)
print('Device Master sheet columns:', devmaster_xls.columns, '\n') #debug, check the columns are right


# import the billing information which will need transforming with work order/ project codes:

data_xls = pd.read_excel('DetailedBilling.xlsx', 'Sheet1', header = [0], index_col = None)
print('Billing sheet columns read in:', data_xls.columns, '\n') #debug, check the columns before

data_xls.insert(13, 'WO Ref', '')
data_xls.insert(14, 'WO Description', '')
data_xls.insert(15, 'Project Code', '')

print('Billing sheet columns with WO additions:', 
data_xls.columns, '\n') #debug, check the columns after

wait = input("Press enter to continue...")


# magic sauce to add work order and cost tracking goes in here
# magic sauce to add work order and cost tracking goes in here
#




data_writer = pd.ExcelWriter('DetailedBilling_edit.xlsx', engine = 'xlsxwriter')
data_xls.to_excel(data_writer, index = False)

#defining the book/sheet to work with
workbook = data_writer.book
worksheet = data_writer.sheets['Sheet1']

# formatting changes
worksheet.set_zoom(85)

server_fmt = workbook.add_format({'font_color': '#800000', 'bold': True})
dollar_fmt = workbook.add_format({'num_format': """_($* #,##0.00_);_($* -#,##0.00;_($* "0.00"_);_(@_)""", 'bold': True})
bold_fmt = workbook.add_format({'bold': True})

worksheet.set_column('A:A', 34, server_fmt)
worksheet.set_column('B:B', 85)
worksheet.set_column('F:F', 28)
worksheet.set_column('G:G', 9)
worksheet.set_column('H:K', 11, dollar_fmt)
worksheet.set_column('L:P', 12.5)
worksheet.set_column('O:O', 85)

#
#what size is this sheet? 
count_row = len(data_xls.index)
#count_row = data_xls.shape[0]
print("Total rows: ", count_row, '\n')

data_writer.save()
我想说的是:

data_xls['WO Ref'].loc[(data_xls['Server'] = devmaster_xls['Device Name'])] = devmaster_xls['WO Ref']
555 19    REF###
Name: WO Ref, dtype: object
556 19    REF###
Name: WO Ref, dtype: object
557 19    REF###
Name: WO Ref, dtype: object
558 19    REF###
Name: WO Ref, dtype: object
559 19    REF###
Name: WO Ref, dtype: object
560 19    REF###
Name: WO Ref, dtype: object
561 19    REF###
Name: WO Ref, dtype: object
562 19    REF###
Name: WO Ref, dtype: object
563 19    REF###
Name: WO Ref, dtype: object
我试着把它放在一个循环中,但还没有走得很远-任何帮助都将不胜感激

编辑:感谢@frankyjuang,我得到了正确的数据,这很好,但由于某些原因,我无法将其写入电子表格。我这样做:

>>> for index, row in data_xls.iterrows(): 
... rowdata = devmaster_xls.loc[devmaster_xls['Device Name'] == row['Server']]
... print(index, rowdata['WO Ref']) 
它看起来很好,返回如下内容:

data_xls['WO Ref'].loc[(data_xls['Server'] = devmaster_xls['Device Name'])] = devmaster_xls['WO Ref']
555 19    REF###
Name: WO Ref, dtype: object
556 19    REF###
Name: WO Ref, dtype: object
557 19    REF###
Name: WO Ref, dtype: object
558 19    REF###
Name: WO Ref, dtype: object
559 19    REF###
Name: WO Ref, dtype: object
560 19    REF###
Name: WO Ref, dtype: object
561 19    REF###
Name: WO Ref, dtype: object
562 19    REF###
Name: WO Ref, dtype: object
563 19    REF###
Name: WO Ref, dtype: object
我试图插入以下内容:

>>> for index, row in data_xls.iterrows(): 
... rowdata = devmaster_xls.loc[devmaster_xls['Device Name'] == row['Server']] 
... row['WO Ref'] = rowdata['WO Ref'] 

但是
print(data_xls['WO Ref'])
显示行为NaN。

通过

for index, row in data_xls.iterrows():
    row['WO Ref']    # Get the data in this way
按查找对应的行

devmaster_xls.loc[devmaster_xls['Device Name'] == some_value]
把它们结合起来

for index, row in data_xls.iterrows():
    the_row_you_want = devmaster_xls.loc[devmaster_xls['Device Name'] == row['WO Ref']]
    # do the operations you want
注: 如果您希望多次执行此操作,则首先创建索引,然后使用
.loc

devmaster_xls = devmaster_xls.set_index(['Device Name'])
devmaster_xls.loc[row['WO Ref']]
更新: 请注意,
rowdata
仍然是一个只有一行的小数据帧。为了获得它的值,您不能直接执行
rowdata['COLUMN']
。而是通过
iloc[0]

row['WO Ref'] = rowdata.iloc[0]['WO Ref'] 
或者,将
iloc[0]
追加到
rowdata=

rowdata = devmaster_xls.loc[devmaster_xls['Device Name'] == row['Server']].loc[0]

由于我缺乏经验,我一直在努力寻找@frankyjuang提供的答案——我无法理解我所得到的结果和我想要实现的结果。因此,经过更多的研究,我提出了以下解决方案,解决了这个问题:

首先,我们需要用一个共享键为两个电子表格编制索引。在本例中,是服务器名,格式为
servername1.com
servername2.com
,等等。但是-我不想永久更改我的数据帧,因此,我将创建一个新列,可以用作索引

这将复制服务器列,将其转换为小写,以考虑以后出现的任何大小写不匹配,并将其设置为索引:

data_xls['Serverindex'] = data_xls['Server'].str.lower() 
data_xls.set_index('Serverindex', inplace = True)
抓取我的设备主控表:

devmaster_xls = pd.read_excel('device_master.xlsx', 'device master', header = [0], index_col = None)
如上所述,从现有列创建索引,同时将其转换为小写:

devmaster_xls['Devindex'] = devmaster_xls['Device Name'].str.lower() 
devmaster_xls.set_index('Devindex', inplace = True)
然后,将相关数据从设备主表复制到主表中非常简单:

data_xls.loc[:,'WO Ref'] = devmaster_xls.loc[:,'WO Ref'] 
data_xls.loc[:,'WO Description'] = devmaster_xls.loc[:,'WO Description'] 
data_xls.loc[:,'Project Code'] = devmaster_xls.loc[:,'Project code']
最后,我们不想写出该索引,因此:

data_xls = data_xls.reset_index(drop = True)
devmaster_xls = devmaster_xls.reset_index(drop = True)

如果这个方法真的很糟糕,我很想找出原因,以及我能做些什么来改进它。不过,它确实解决了问题,而且实现起来很快

谢谢@frankyjuang-这给了我正确的数据,这很好,但由于某些原因,我无法将其写入电子表格。我这样做:
>>对于索引,数据中的行。\u xls.iterrows():。。。rowdata=devmaster_xls.loc[devmaster_xls['Device Name']==row['Server']]。。。打印(索引,行数据['WO Ref'])
看起来不错。我正试图插入以下内容:
>>对于索引,数据中的行。\u xls.iterrows():。。。rowdata=devmaster_xls.loc[devmaster_xls['Device Name']==row['Server']]。。。行['WO Ref']=rowdata['WO Ref']
但是
打印(数据['WO Ref'])
行是NaN..这不是很可读,抱歉,希望清楚。。。评论只能编辑5分钟>:(答案已更新。如果您觉得有帮助,请毫不犹豫地选择我作为最佳答案。不幸的是,这给了我一个错误-
对于索引,data_xls.iterrows()中的行:
rowdata=devmaster_xls.loc[devmaster_xls['Device Name']==row['Server']]
行['WO-Ref']=rowdata.iloc[0]['WO-Ref']
错误是
索引器:单个位置索引器超出了范围
。如果没有匹配的行,则会引发错误。首先检查是否有匹配的行。你能将更新放在帖子中吗?评论中确实不可读:(完成,干杯@frankyjuang