我正在尝试设置python脚本,以获取我组装的一组数据,以便基于if/then语句自动将文件从源位置移动到其他几个文件夹。
理想情况下,代码应该是1.)读取dataframe并检查其中两个列是否匹配设置的文本输入2)。遍历dataframe中的每一行,以确定哪些行满足这些条件3)。读取正确行的第一列中的文本,并连接要传输到的源文件的文件位置。
以下是我目前的情况:
import pandas as pd
import numpy as np
import shutil
df = pd.read_excel(r'\\KCOW00\Jobs\72046\Design\Bridges\Statewide Bridge Data\z_Scratch\CCD\Python Tests\data.xlsx')
src = r'\\KCOW00\Jobs\72046\Design\Bridges\Statewide Bridge Data'
yr8 = r'\2022_Year 8'
auto = r'\Field Investigation Reference\Automation'
culv = r'\KLBRP - CBWOP Field Sketch Forms - Culvert.pdf'
grdr = r'\KLBRP - CBWOP Field Sketch Forms - Girder Bridge.pdf'
slab = r'\KLBRP - CBWOP Field Sketch Forms - Slab Bridge.pdf'
br_num = df['NBI Bridge Number']
district = df['District']
county = df['County']
owner = df['Owner Group']
type = df['Type']
br_type = df['BR_Type']
span = df['Span']
def Add_Forms:
if (type == 'CBWOP') & (br_type == '19-Culvert'):
shutil.copy2(src + auto + culv, src + yr8 + '\\' + district + '\\' + county + '\\' + owner + '\\' + br_num + '\\' + 'Field Investigation' + culv)
elif (type == 'CBWOP') & (br_type == '01-Slab'):
shutil.copy2(src + auto + slab, src + yr8 + '\\' + district + '\\' + county + '\\' + owner + '\\' + br_num + '\\' + 'Field Investigation' + slab)
elif (type == 'CBWOP') & (br_type == '02-Stringer/Girder'):
shutil.copy2(src + auto + grdr, src + yr8 + '\\' + district + '\\' + county + '\\' + owner + '\\' + br_num + '\\' + 'Field Investigation' + grdr)
Add_Forms我目前正在接收一个ValueError,因为真值是不明确的。谢谢你的帮助。
发布于 2022-08-09 19:23:49
不能在没有示例数据的情况下测试此解决方案,但是您应该能够使用这种方法,在Type和BR_Type列中遍历值组合,并从相应行的其他列中获取值。
如果您的数据已经没有简单的顺序行索引(0, 1, 2, 3, ...),那么首先运行df = df.reset_index(drop=True)来重置它。
for idx, (t, bt) in enumerate(zip(df['Type'], df['BR_Type'])):
if (t == 'CBWOP') & (bt == '19-Culvert'):
district = df.loc[idx, "District"]
county = df.loc[idx, "County"]
owner = df.loc[idx, "Owner Group"]
br_num = df.loc[idx, "NBI Bridge Number"]
shutil.copy2(
src + auto + culv, src + yr8 + '\\' + district + '\\' + county +
'\\' + owner + '\\' + br_num + '\\' + 'Field Investigation' + culv
)
elif (t == 'CBWOP') & (bt == '01-Slab'):
# implement the same logic as above
elif (t == 'CBWOP') & (bt == '02-Stringer/Girder'):
# implement the same logic as abovehttps://stackoverflow.com/questions/73284398
复制相似问题