1

I want to iterate each row of df2['name]' if the coin name is also found in df1 I want to create a new row in df2 with the tag names of df1. Alternatively create a new Df3 which ever the easiest.

I have tried many solution such as iterating but I had no luck

for row in df2['name']:
  
   for row2 in df1['name']:
   
      if row == row2:
        ...cannot complete this step...

My main issue it is that I don't know what is the best methodology to approach this. Iterations or not? I am sure that pandas has a better method but I cannot work it out.

I have a pandas dataframe that looks like that

df1

name tags
1 bitcoin mineable
2 ethereum pow

and a second one like this

df2

name value
1 bitcoin 0
2 ethereum 0.242424

Df3 the one I want to create

name value tags
1 bitcoin 0 mineable
2 ethereum 0.242424 pow
Emilia Delizia
  • 333
  • 3
  • 14
  • Hello, instead of iterating, you can use ```pandas.merge``` to join dataframes based on defined key which is ```name``` in your case. https://pandas.pydata.org/docs/getting_started/comparison/comparison_with_sql.html#inner-join – s3nh Jun 28 '21 at 07:23

1 Answers1

1

There is no need to iterate

import pandas as pd
from io import StringIO

df1 = pd.read_csv(StringIO("""name  tags
bitcoin     mineable
ethereum    pow"""), sep="\s+")


df2 = pd.read_csv(StringIO("""name  value
bitcoin     0
ethereum    0.242424"""), sep="\s+")

>>> df1.merge(df2, on="name")
       name      tags     value
0   bitcoin  mineable  0.000000
1  ethereum       pow  0.242424
crayxt
  • 2,367
  • 2
  • 12
  • 17