Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to modify data frame row value
#1
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')

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Handling escape charters while converting data frame to JSON RahulShukla 0 149 Nov-11-2019, 11:22 AM
Last Post: RahulShukla
  Loop pandas data frame by position ? Johnse 1 362 Sep-06-2019, 12:26 AM
Last Post: scidam
  pandas data frame dervast 1 297 Aug-28-2019, 12:40 PM
Last Post: ThomasL
  Insert Pandas Data Frame into Teradata DB kylenater 0 1,306 Jul-19-2019, 04:53 PM
Last Post: kylenater
  Creating new rows and adding them to empty data frame kapilan15 0 369 May-31-2019, 10:19 AM
Last Post: kapilan15
  Getting the difference between two times in a Data Frame soulkid555 4 586 Mar-21-2019, 11:22 AM
Last Post: perfringo
  save video frames into pandas data-frame tofi 0 605 Oct-18-2018, 07:02 PM
Last Post: tofi
  Best way to append data frame? WuchaDoin 1 726 Oct-04-2018, 07:17 PM
Last Post: volcano63
  Reading json file as pandas data frame? Alberto 1 5,524 Feb-05-2018, 12:43 AM
Last Post: snippsat
  Panda Data Frame to Existing Multiple Sheets naveedraza 1 3,418 Jul-11-2017, 12:21 PM
Last Post: naveedraza

Forum Jump:


Users browsing this thread: 1 Guest(s)