'how to replace the particular column value using python

Input Data Frame

output data frame in output, some particular column should be replace and other value of other column should be repeated



Solution 1:[1]

If your input dataframe has column names as col1, col2, col3 and col4, the required output dataframe can be generated as follows:

output_df = pd.DataFrame(columns = ['Name', 'Val1', 'Val2', 'Val3'])
for i,j,k,l in zip(df['col1'], df['col2'], df['col3'], df['col4']):
    if 'NTR' in i:
        j = 19.0
        k = 0.0
        i.replace('NTR', 'NT')
        if int(i[-2:]) % 2 == 0:
            l = 720
        else:
            l = 710
    if 'PHB' in i:
        j = 126.0
        k = 0.0
        if int(i[-2:]) % 2 == 0:
            l = 630
        else:
            l = 620
    if 'GHY' in i:
        j = 62.0
        k = 0.0
        if int(i[-2:]) % 2 == 0:
            l = 750
        else:
            l = 740
    if 'TRP' in i:
        i.replace('TRP', 'TR')
        j = 50.0
        k = 2.0
        if int(i[-2:]) % 2 == 0:
            l = 130
        else:
            l = 120
    upd = {'Name': i,'Val1' : j, 'Val2' : k,'Val3' : l}
    output_df = output_df.append(upd, ignore_index = True)

Where df is your input dataframe and output_df is your resultant dataframe.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 karthik_ghorpade