0

I am new to Python and so need help with creating multiple csvs from 1 csv for a value in 1 column. My original csv file sample below. The original file is 1000 lines big.

Path                              FolderName    FolderOwner    EmailAddress
\\A\folder1\subfolder1\file1      folder1       Roy            [email protected]
\\A\folder2\subfolder4\file7      folder2       Roy            [email protected]
\\A\folder3\subfolder11\file9     folder3       Jack           [email protected]
\\A\folder4\subfolder13\file12    folder4       Jack           [email protected]

For above example I want to run a for loot to create 2 new csv files for FolderOwner = Roy and Jack. In original csv file I got 50 FolderOwners. Once I have these CSV files, I want to send automatic email to the owners using EmailAddress folder, but that's part 2.

First I need a python script with for loop to automatically create 2 csv files for Roy and Jack.

I have below script, but for it I need to give FolderOwner manually, which is hard for 50 owners.

df1 = pd.read_csv ('originalfile.csv')
df2 = df1[df1.FolderOwner == 'Roy']
df3 = df2['Path', 'FolderName', 'FolderOwner']
df3.to_csv('Royfile.csv')
print(df3)

1 Answer 1

1

try this,

df1 = pd.read_csv ('originalfile.csv')

for fn in df1.FolderOwner.unique():
    (df1.loc[df1.FolderOwner.eq(fn),
             ['Path', 'FolderName', 'FolderOwner']].to_csv(f'{fn}file.csv'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.