Python Forum
How to modify data frame row value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to modify data frame row value (/thread-24408.html)



How to modify data frame row value - SriRajesh - Feb-12-2020

Hi,
I have dataframe:
df3:
name type score combined
A    t1    0    A_t1
B    t2    0    B_t2
C    t2    1    C_t2   
A    t2    1    A_t2
C    t1    0    C_t1  
B    t3    0    B_t3
summary_table:
name t1 t2 t3
A    na  na na
B    na  na na
C    na  na na
I loop through rows summary_table (A,B,C) & columns (t1,t2,t3)
if name & value exist in df3, the replace data corresponding row value with row value in df3.
exampe, A &t1 exist, its value is 0 in df3.
final desired output:

name t1 t2 t3
A    0  1 na
B    na  0 0
C    0  1 na
I use the below code, but I am not getting the output.

import pandas as pd
import numpy as np

myrws=sorted(list(data1.name.unique()))
mycols=sorted(list(data1.category.unique()))
lenrows=len(myrws)

for i in range(1,len(myrws)):
    c1=myrws[i]
    print(myrws[i])
    print("-"*10)
    for j in range(1,len(mycols)):
        print(1,j)
        print(mycols[j])
        r1=mycols[j]
        tmp=c1+"_"+r1
        print("tmp:",tmp)
        idx=(df3[df3['combined']==tmp].index.values)
        if idx:
            print('matched-----')
            summary_table.loc[i,j]=df3.iloc[idx,3]
        else:
            print('nottt matched')