-1

I am running one python script. I want to change particular row value without changing the other value. Can you please help me how to do this?

example:

df1
Table           Count
case            20
recordtype      50
consumer        70
settlement      150
address         250
bridge          130

I ran the process for only 'case' & 'consumer' job. Now new count in case and consumer is 80 & 150 but i am getting the file like this.

Table           Count
case            20
recordtype      50
consumer        70
settlement      150
address         250
case            80
consumer        150

It is not replacing the count value. It is just adding new column. But i want result like this:

df

Table           Count
case            80
recordtype      50
consumer        150
settlement      150
address         250

Can you please help me based on the Table name how can i replace the value?

I am using the below code:

if(os.path.isfile('/medaff/Scripts/python/count.txt')):
            df_s.to_csv('/medaff/Scripts/python/count.txt',mode='a', sep = '|', index= False, header=False)
else:
            df_s.to_csv('/medaff/Scripts/python/count.txt', sep = '|', index= False)
shivam
  • 75
  • 1
  • 3
  • 11
  • seems like subject is misleading..please clear things here..what you have and what you are trying to achieve..also `I ran the process for only 'case' & 'consumer' job` ? – iamklaus Apr 09 '20 at 10:17
  • @iamklaus I am running the process based on Active_flg. So i put Active_flg = 'Y' for only case and consumer. So only two job will run means run the sql query and generate the output in dataframe then i will take the count. – shivam Apr 09 '20 at 10:29

1 Answers1

0

I fixed the issue using below code:

if(os.path.isfile('/medaff/Scripts/python/count.txt')):
            df_s1 = pd.read_csv('/medaff/Scripts/python/count.txt', delimiter='|')
            for index, row in df_s.iterrows():
                print(row)
                print(row['Master Job Name'])
                print(row['Current_Count'])
                idx1=(df_s1['Master Job Name'] == row['Master Job Name'])

                df_s1.at[idx1, 'Current_Count'] = row['Current_Count']
            set1 = set(list(df_s['Master Job Name'].unique()))
            set2 = set(list(df_s1['Master Job Name'].unique()))
            set1 = list(set1 - set2)

            df_s_new = df_s[df_s['Master Job Name'].isin(set1)]

            df_s1 = df_s1.append(df_s_new)
            df_s1.to_csv('/medaff/Scripts/python/count.txt', sep='|', index=False)

else:
        df_s.to_csv('/medaff/Scripts/python/count.txt', mode='a', sep = '|', index= False)
shivam
  • 75
  • 1
  • 3
  • 11