IriSandivel
IriSandivel

Reputation: 103

Change only a fraction of value for an entire column in dataframe based on another dataframe

Assuming I have a dataframe like the following:

df1
     Products    Cost
 0     rice        12
 1     beans       15
 2     eggs        17
 3     Tomatoes    5

And I have another dataframe with the same headers but that has a certain number which causes the quantity of numbers to become "a" letters. Exemplifying.

df2
        Header     quantity
 0      Products       2
 1      Cost           1

Which should give me a result like this:

df3
     Products    Cost
 0    riaa        1a
 1    beaaa       1a
 2    egaa        1a
 3    Tomatoaa    NaN

How should this case be resolved? I do not know if the "replace" method works

Upvotes: 2

Views: 44

Answers (1)

BENY
BENY

Reputation: 323326

Using map, after create the mapdict

d={x:x[:-y]+'a'*y for x, y  in zip(df2.Header,df2.quantity)}
df1.Products=df1.Products.map(d)
df1
Out[863]: 
   Products  Cost
0      riaa    12
1     beaaa     3
2      egaa     2
3  Tomaaaaa    11

Upvotes: 2

Related Questions