根据destination_country 列将 NA 值插入到带有国家/地区首都的 Destination location 列中

发布于 2025-01-10 15:46:12 字数 1749 浏览 0 评论 0原文

我在 shipcat_df 中有 2 列 - Destination LocationDestination_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文