-1

I'm working with dataframes on Python, I'm having trouble joining two dataframes. A dataframe is related to information from countries around the world. Another dataframe is related to the detailed information of one of the countries of the world of the previous dataframe (i.e. I'm going to add information on the regions, provinces, municipalities). I created a script that merged the headers of the two data frames, the problem now is how to make a very distant comparison between the information of the second dataframe only in the field of the country in question?

My idea of a pseudo code was to first add 'len (dataframe2)' lines equal to dataframe1 related to the country information of which I then have the second detailed dataframe. How do I get that information right? This is what I would like to do (see photos)

enter image description here

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • Have you tried [`DataFrame.merge`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html?highlight=merge#pandas-dataframe-merge)? – 0x5453 Jun 02 '20 at 18:13
  • Does this answer your question? [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – r.ook Jun 02 '20 at 18:15

1 Answers1

0

Make sure you have one column that's identical in both CSV files. I would add a column to the CSV with data of one country and enter the name of that country in each cell. Let's call the header of that column "country_name". Then do this:

countries = pd.read_csv(filepath)
one_country = pd.read_csv(filepath)

merged = pd.merge(countries, one_country, on=["country_name"], how="inner")
Remco Mooij
  • 43
  • 1
  • 1
  • 7