Python Forum
pandas change value two dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas change value two dataframe
#1
good morning i'm trying to find the differences between two dataframes the promo 'df1' has only one column which is 'codbar, where the equivalent in' def2 'and' 'sku' in practice I have to find the equivalences of codbar in the sku column for then change the values on the other two columns stock_quantity and stok_status. i'm trying but i can't do it i need help

import pandas as pd




data1 = pd.read_csv('eurogold.csv' )

data2 = pd.read_csv('woo.csv')

df1 = pd.DataFrame(data1,columns=['codbar'])
df2 = pd.DataFrame(data2,columns=['sku','stock_quantity','stock_status'])


print(df1.equals(df2))
Reply
#2
have tried this, but not work well.

import pandas as pd

df1 = pd.DataFrame({'codbar': ['k5', 'K3', 'k2']})

df2 = pd.DataFrame({'stock_status': ['C0', 'C2', 'C3','cd'],
                    'stock_quantity': ['D0', 'D2', 'D3', 'gg'],
                    'sku': ['K0', 'K2', 'K3', 'k5']})
print(df2)

def change_cols(x):
    if x['sku'] in df1.codbar.tolist():
        x['stock_quantity'] = '1'
        x['stock_status'] = '2'


df2.apply(change_cols, axis=1)

print(df2)
Reply
#3
Big Grin
You capitalized only 'K3':
'codbar': ['k5', 'K3', 'k2']

and sku 'k5' not capitalized
'sku': ['K0', 'K2', 'K3', 'k5']
Reply
#4
ok you are right my mistake. but there I tested on my datframes extracted from two csv but no code changes (the first two codes are the same in all and two tables) I share my project. the script in question is testDataframe.py

GIT
Reply
#5
I have found a solution:

import pandas as pd


data1 = pd.read_csv('eurogold.csv',index_col=0)

data2 = pd.read_csv('woo.csv', index_col=0)

df1 = pd.DataFrame(data1)

print ('stampa df1')
df1 = df1.drop_duplicates(['codbar'])  # elimina duplicati
df1['codbar'] = df1['codbar'].astype(str)
print(df1)

df2 = pd.DataFrame(data2)

df2 = df2.dropna().reset_index(drop=True)
print ('stampa df2')
print(df2)


mask = df2.sku.isin(df1.codbar.values)
df2.loc[mask, 'stock_quantity'] = '0'
df2.loc[mask, 'stock_status'] = 'outofstock'
print(df2)

df2.to_csv('risultato.csv')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Question on pandas.dataframe merging two colums shomikc 4 783 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,303 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  help how to get size of pandas dataframe into MB\GB mg24 1 2,231 Jan-28-2023, 01:23 PM
Last Post: snippsat
  Change a numpy array to a dataframe Led_Zeppelin 3 1,066 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  pandas dataframe into csv .... exponent issue mg24 10 1,708 Jan-20-2023, 08:15 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 799 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,697 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  How to change UTC time to local time in Python DataFrame? SamKnight 2 1,528 Jul-28-2022, 08:23 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 1,796 May-09-2022, 01:35 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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