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
  how to filter data frame dynamically with the columns psahay 0 123 Aug-24-2020, 01:10 PM
Last Post: psahay
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 228 Aug-03-2020, 02:05 AM
Last Post: scidam
  How to shift data frame rows of specified column Mekala 0 381 Jul-21-2020, 02:42 PM
Last Post: Mekala
  HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 214 Jul-11-2020, 09:04 PM
Last Post: bntayfur
  Pandas data frame creation from Kafka Topic vboppa 0 159 Jul-01-2020, 04:23 PM
Last Post: vboppa
  Filter rows by multiple text conditions in another data frame i.e contains strings an Pan 0 424 Jun-09-2020, 06:05 AM
Last Post: Pan
  Displaying Result from Data Frame from Function eagle 1 402 Apr-08-2020, 11:58 PM
Last Post: eagle
  add formatted column to pandas data frame alkaline3 0 305 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  reformatting data frame tkirkland 0 239 Mar-18-2020, 02:17 PM
Last Post: tkirkland
  Handling escape charters while converting data frame to JSON RahulShukla 0 374 Nov-11-2019, 11:22 AM
Last Post: RahulShukla

Forum Jump:


Users browsing this thread: 1 Guest(s)