Python Forum
How to modify data frame row value
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 607 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  Filtering Data Frame, with another value NewBiee 9 1,331 Aug-21-2023, 10:53 AM
Last Post: NewBiee
  Exporting data frame to excel dyerlee91 0 1,604 Oct-05-2021, 11:34 AM
Last Post: dyerlee91
  Pandas Data frame column condition check based on length of the value aditi06 1 2,655 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  Adding a new column to a Panda Data Frame rsherry8 2 2,083 Jun-06-2021, 06:49 PM
Last Post: jefsummers
  grouped data frame glitter 0 1,577 Feb-02-2021, 11:22 AM
Last Post: glitter
  how to filter data frame dynamically with the columns psahay 0 2,378 Aug-24-2020, 01:10 PM
Last Post: psahay
  Dropping Rows From A Data Frame Based On A Variable JoeDainton123 1 2,186 Aug-03-2020, 02:05 AM
Last Post: scidam
  How to shift data frame rows of specified column Mekala 0 1,860 Jul-21-2020, 02:42 PM
Last Post: Mekala
  HELP- DATA FRAME INTO TIME SERIES- BASIC bntayfur 0 1,732 Jul-11-2020, 09:04 PM
Last Post: bntayfur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020