0

I'm trying to set up a python script to take a set of data I've assembled to automate the movement of files from a source location to several other folders based on an if/then statement.

Ideally, the code would 1.) read the dataframe and check to see if two of the columns match a set text input 2.) iterate through each of the rows in the dataframe to determine which rows satisfy the these criteria 3.) read the text in first column of the correct rows and concatenate the file location for the source file to be transferred to.

Here is what I currently have:

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

I'm currently receiving a ValueError because the truth value is ambiguous. Thanks for the help.

  • Welcome to Stack Overflow. Currently, your question cannot be adequately answered, because we don't know anything about the contents or the structure of your dataframe. Please take some time to read [how to create a minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391) and then [edit] your post to include sample input and expected output. Also, for debugging help, always include the entire traceback output in the body of your post, in text format. – AlexK Aug 09 '22 at 07:10
  • At first glance, you are getting this error because you are comparing entire pandas columns (Series objects) to string constants (e.g., `br_type == '19-Culvert'`). You have other issues in the code you posted: you should not use a built-in function name like `type` to name variables, and there are syntax problems with your function definition. A function's name should be followed by parentheses (and if you are really writing a function, it should have arguments and a return value). – AlexK Aug 09 '22 at 07:13
  • Apologies for the lack of an adequate input. I can create a more reproducible example later on and see if that helps. In the meantime - is there a fix for the issue related to comparing pandas columns to string constants? Is there a proper way to execute an “if/then” on each individual dataframe row and have it return either “True” or “False,” and then have my shutil function executed for each row that returned “True?” – Duffalo Aug 09 '22 at 10:43

1 Answers1

0

Can't test this solution without sample data, but you should be able to use this approach, where you loop over combinations of values in the Type and BR_Type columns and obtain values from the other columns for the corresponding row.

If your dataframe does not have a simple sequential row index already (0, 1, 2, 3, ...), first run df = df.reset_index(drop=True) to reset it.

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 above
AlexK
  • 2,855
  • 9
  • 16
  • 27