Assumption
I want to reference and match two files and add new columns and numbers to one of the files.
What we want to do
If the name in the "Name" column of the first file (wals_languages_joshi.csv) matches the name in the "name" column of the second file (Joshi2020_list.csv), the "score" column in the second file to the first file. (See below).
When this code is run, nothing is added to the newly added "name" and "score" columns. I'd like to ask for your help. Thank you in advance.
Error message
Traceback (most recent call last):
File "/Users/carlobroschi_imac/Workspace/wals_and_joshi.py", line 8, in <module>
after_pd = after_pd.capitalize()
File "/opt/homebrew/lib/python3.9/site-packages/pandas/core/generic.py", line 5575, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'capitalize'
carlobroschi_imac@CarlonoiMac Workspace % python3 wals_and_joshi.py
Traceback (most recent call last):
File "/Users/carlobroschi_imac/Workspace/wals_and_joshi.py", line 8, in <module>
after_pd = after_pd.str.capitalize()
File "/opt/homebrew/lib/python3.9/site-packages/pandas/core/generic.py", line 5575, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'str'
Source code
#!/usr/bin/env python3.8
# -*- coding: utf-8 -*-
import pandas as pd
before_pd = pd.read_csv('wals_languages_joshi.csv')
after_pd = pd.read_csv('Joshi2020_list.csv')
after_pd = after_pd.str.capitalize()
f=pd.merge(before_pd, after_pd, left_on='Name', right_on='Name', how='outer')
f.to_csv('result2.csv',index=False, encoding='utf-8')
ideal output
Name,Family,Subfamily,Genus,Macroarea,ISO639P3code,score
Arapesh (Abu),Torricelli,,Kombio-Arapesh,,
Aari,Afro-Asiatic,Omotic,South Omotic,Africa,aiw,0
Abau,Sepik,,Upper Sepik,Papunesia,aau,0
Arabic (Chadian),Afro-Asiatic,,Semitic,Africa,shu
Abidji,Niger-Congo,,Kwa,Africa,abi
Files (wals_languages_joshi.csv,Joshi2020_list.csv)
Name,Family,Subfamily,Genus,Macroarea,ISO639P3code
Arapesh (Abu),Torricelli,,Kombio-Arapesh,,
Aari,Afro-Asiatic,Omotic,South Omotic,Africa,aiw
Abau,Sepik,,Upper Sepik,Papunesia,aau
Arabic (Chadian),Afro-Asiatic,,Semitic,Africa,shu
Abidji,Niger-Congo,,Kwa,Africa,abi
name,score
a-pucikwar,0
aari,0
a-pucikwar,0
abaza,0
abenaki,0
Additional information
iMac, Python3.9.12, VSCode