0

I am working on a python program which connects to salesforce and downloads the data in the form of a csv file. Salesforce doesn't allow aliasing the column. So I am using a config file to alias the column name. I need to replace only few column names not all. I tried using for loop in the code, it works if there is one column name in the config file but it does not rename if there are multiple columns

My code looks like this:

import configparser
cfg = configparser.RawConfigParser(allow_no_value=True)
cfg.read_file(open(r".\User_Info.config"))

username=cfg['SFDC_auth']['username']
password=cfg['SFDC_auth']['password']
security_token=cfg['SFDC_auth']['security_token']
domain=cfg['SFDC_auth']['domain']

Stories_Query=cfg['SFDC_Queries']['Stories_Query']

from simple_salesforce import Salesforce

sf_data=sf.query_all(Stories_Query)
sf_df = pd.DataFrame(sf_data['records']).drop(columns='attributes')

st_src_val=cfg['Column_Alias']['Stories_Src_Col']
st_dst_val=cfg['Column_Alias']['Stories_Tgt_Col']

if st_src_val =="" or st_dst_val=="":
    print("No columns to Rename in Stories")
    sf_df.to_csv(local_dir+"\PF_Stories.csv", index=False, encoding='utf8')
else:
    st_src=list(cfg['Column_Alias']['Stories_Src_Col'].split(','))
    st_dst=list(cfg['Column_Alias']['Stories_Tgt_Col'].split(','))
    for i,j in zip(st_src,st_dst):
            df2 = sf_df.rename({i:j},axis=1)
            print(i + ' got renamed to ' + j + ' in Stories')
    df2.to_csv(local_dir+"\PF_Stories.csv", index=False, encoding='utf8')

Config File looks like this:

[SFDC_Queries]
Stories_Query=Select Id,Name,PF_Story_Number__c,toLabel(PF_Story_Status__c),PF_Total_Estimated_Hours__c,PF_Total_Actual_Hours__c,PF_End_Date__c,PF_Start_Date__c,PF_Story_Point__c,Assigned_Developer__c,PF_Sprint__c,PF_Planned_Start_Date__c,PF_Planned_End_Date__c,PF_Revised_Start_Date__c,PF_Revised_End_Date__c,PF_Work_Stream__c,PF_Scope_Changed__c,PF_Scope_Changed_Number__c,PF_Percentage_Completion__c from PF_Stories__c

[Column_Alias]
Stories_Src_Col=Assigned_Developer__c
Stories_Tgt_Col=PF_Story_Owner__c

The code works fine if I have only one value to be replaced but if I use multiple Values as shown below then it does not replace any column headers in the file. How to resolve this ?

[Column_Alias]
Stories_Src_Col=Assigned_Developer__c,id
Stories_Tgt_Col=PF_Story_Owner__c,id1
Bhaskar
  • 35
  • 6
  • this Q&A should help: https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas?rq=1 – Ed Kloczko Jun 17 '20 at 12:42
  • It is working if I have only one column in the Stories_Src_Col and Stories_Tgt_Col but not working if I have multiple columns. Not sure why looping is not renaming the column names – Bhaskar Jun 17 '20 at 12:48
  • don't loop through the renaming, use a list of column names that you build through looping and rename it all at once – Ed Kloczko Jun 17 '20 at 12:50

1 Answers1

0

Thank you edkloczko, I found the answer, I just changed the code to

if st_src_val =="" or st_dst_val=="":
    print("No columns to Rename in Stories")
    sf_df.to_csv(local_dir+"\PF_Stories.csv", index=False, encoding='utf8')
else:
    st_src=list(cfg['Column_Alias']['Stories_Src_Col'].split(','))
    st_dst=list(cfg['Column_Alias']['Stories_Tgt_Col'].split(','))
    sf_df.rename(columns=dict(zip(st_src, st_dst)), inplace=True)
    for i,j in zip(st_src,st_dst):
            print(i + ' got renamed to ' + j + ' in Stories')
    sf_df.to_csv(local_dir+"\PF_Stories.csv", index=False, encoding='utf8')

It is working now as expected

Bhaskar
  • 35
  • 6