0

I'm trying to merge two CVS files by column but getting error.


import os
import pandas as pd

os.chdir('/home/yovel/PycharmProjects/fantasyfinal')

a = pd.read_csv("statsmerger.csv")
b = pd.read_csv("team.csv")
b = b.dropna(axis=1)
merged = a.merge(b, on = 'player')
merged.to_csv("output1.csv", index=False)

I expect the new file to fill the empty column in the first column. i'm getting KeyError: 'player'

  • Make sure your two dataframes contain `player` column – shaik moeed Oct 21 '19 at 04:28
  • [`merge`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html#pandas-dataframe-merge), if they have a common column, use `on`. Otherwise, use `left_on` and `right_on`. – Trenton McKinney Oct 21 '19 at 04:32
  • 2
    Please [provide a reproducible copy of the DataFrame with `to_clipboard`](https://stackoverflow.com/questions/52413246/provide-a-reproducible-copy-of-the-dataframe-with-to-clipboard/52413247#52413247) of both dataframes. This isn't a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Trenton McKinney Oct 21 '19 at 04:35
  • can you add a sample of both the dataset with the column name? – Arun AK Oct 21 '19 at 04:51

1 Answers1

0

One of your dataframes doesn't seem to have the player column. You can check this by using a.columns and b.columns to see the columns' names.

Be aware that when you use

b = b.dropna(axis=1)

and player has at least one missing value, it will drop the entire column. To avoid that, you can use

b = b.dropna()

instead, and it will drop the rows.