根据destination_country 列将 NA 值插入到带有国家/地区首都的 Destination location 列中
我在 shipcat_df
中有 2 列 - Destination Location
和 Destination_Country
Destination Location
列中有许多 NA 值,我尝试根据 Destination_Country
列将它们替换为大写字母。以下是我尝试使用的代码:
from countryinfo import CountryInfo
shipcat_df['Destination Location'] = shipcat_df['Destination Location'].replace({'NA': [CountryInfo(shipcat_df['Destination_Country'])].capital()}, inplace=True)
我收到错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_26188/610543798.py in <module>
1 ##Creating a country capital column
2 from countryinfo import CountryInfo
----> 3 shipcat_df['Destination_Location'] = shipcat_df['Destination
Location'].replace({'NA': [CountryInfo(shipcat_df['Destination_Country'])].capital()}, inplace=True)
~\AppData\Roaming\Python\Python39\site-packages\countryinfo\countryinfo.py in __init__(self, country_name)
20 pass country name
21 """
---> 22 self.__country_name = country_name.lower() if country_name else ''
23 # get the files path
24 __file_dir_path = dirname(realpath(__file__))
C:\Program Files (x86)\Anaconda\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1535 @final
1536 def __nonzero__(self):
-> 1537 raise ValueError(
1538 f"The truth value of a {type(self).__name__} is ambiguous. "
1539 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
请帮助。谢谢
I have 2 columns in shipcat_df
- Destination Location
and Destination_Country
There are many NA values in the Destination Location
column and I am trying to replace them with their capital based on Destination_Country
column. Below is the code I tried to use:
from countryinfo import CountryInfo
shipcat_df['Destination Location'] = shipcat_df['Destination Location'].replace({'NA': [CountryInfo(shipcat_df['Destination_Country'])].capital()}, inplace=True)
I get the error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_26188/610543798.py in <module>
1 ##Creating a country capital column
2 from countryinfo import CountryInfo
----> 3 shipcat_df['Destination_Location'] = shipcat_df['Destination
Location'].replace({'NA': [CountryInfo(shipcat_df['Destination_Country'])].capital()}, inplace=True)
~\AppData\Roaming\Python\Python39\site-packages\countryinfo\countryinfo.py in __init__(self, country_name)
20 pass country name
21 """
---> 22 self.__country_name = country_name.lower() if country_name else ''
23 # get the files path
24 __file_dir_path = dirname(realpath(__file__))
C:\Program Files (x86)\Anaconda\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1535 @final
1536 def __nonzero__(self):
-> 1537 raise ValueError(
1538 f"The truth value of a {type(self).__name__} is ambiguous. "
1539 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Please help. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论