-1

We have two dataframes exported from Excel. Both have a column called "PN", which was set at the exporting. "First" and "Second" are the variables with those dataframes. "Third" stores a list of coinsidences between the 2 "PN" columns. Pandas Merge method worked without such list, but since the thing now is not working, I added it as well.

gnida = []
for h in first['PN']:
        for u in zip(second['PN'], second['P']):
             if h==u[0]:
                 gnida.append(u)
third = pd.DataFrame(gnida)

I need values in the second dataframe to be placed on the rows where coinsidence occurs. If I simply merge:

fourth = first.merge(second)

, columns that have names other than in the first df are added, but the output is 1 row of headings without rows with values. If I merge

fourth = first.merge(third)

, I get: No common columns to perform merge on. Merge options: left_on=None, right_on=None, left_index=False, right_index=False. If I state further "left on = "PN", I get: object of type 'NoneType' has no len().

Thus, how can Merge or Join or whatever the 2 dataframes in order to use one column of the second dataframe as a key, placing values in a new column where coinsidence occurs. Thank you

Fareed
  • 33
  • 4

1 Answers1

0

if you wish to merge by the index, just use fourth = first.join(third) otherwise, you need to create a dataframe from third, add the column that you want to merge by, and use:

fourth = first.merge(third,on='name_of_the_column')
David
  • 871
  • 1
  • 5
  • 13
  • I stated "PN" as agroment to Merge, since the coinsidences need to be found there, but got as output "keyerror "PN", though both dfs have such column – Fareed Feb 02 '22 at 10:26