简体   繁体   English

如何根据其他事实从不同表中选择列以创建新的数据框python

[英]How to select columns from different tables based on other facture to create a new dataframe python

I have 2 DataFrames both countain countries 1-first have 183 row 2-the second have 156 row both of them has import information on each other I need one column from the first and one column from the second My goal is to create a single Dataframe contain both columns that I need and name of the contain that both datafames commo.我有 2 个数据帧,两个国家/地区 1-第一个有 183 行 2-第二个有 156 行,它们都有彼此的导入信息我需要第一列中的一列和第二列中的一列我的目标是创建一个数据帧包含我需要的两列和包含两个 datafames commo 的名称。

This is what I did and the message that I got这就是我所做的以及我得到的信息

for i in range(183) :
    for j in range(156):
        if df['Country'][i]==df_happy['Country or region'][j]:
            df.drop(i,axis=0,inplace=True) 
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-25-e078ef71e219> in <module>
      1 for i in range(183) :
      2     for j in range(156):
----> 3         if df['Country'][i]==df_happy['Country or region'][j]:
      4             df.drop(i,axis=0,inplace=True)

/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/core/series.py in __getitem__(self, key)
    869         key = com.apply_if_callable(key, self)
    870         try:
--> 871             result = self.index.get_value(self, key)
    872 
    873             if not is_scalar(result):

/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
   4403         k = self._convert_scalar_indexer(k, kind="getitem")
   4404         try:
-> 4405             return self._engine.get_value(s, k, tz=getattr(series.dtype, "tz", None))
   4406         except KeyError as e1:
   4407             if len(self) > 0 and (self.holds_integer() or self.is_boolean()):

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1

You can merge both data frames:您可以合并两个数据框:

newdf=df.merge(df_happy,how='left', left_on='Country', right_on='Country or region')

and then drop the extra columns with:然后删除额外的列:

newdf.drop(columns=['B', 'C'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于来自不同数据框的其他列创建新列 - Create new column based on other columns from a different dataframe 根据多个其他列的条件新建 Python DataFrame 列 - Create new Python DataFrame column based on conditions of multiple other columns 如何基于其他数据帧的列使用 pandas 创建新的 dataframe - How to create new dataframe with pandas based on columns of other dataframes 如何根据其他列的值在数据框中创建新列? - How to create a new column in a dataframe based off values of other columns? 如何从数据框中的其他列创建新的Pandas数据框列 - How to create a new Pandas dataframe column from other columns in the dataframe 如何根据 Pandas DataFrame 中其他列的值创建新列 - How to create a new column based on values from other columns in a Pandas DataFrame 如何根据其他 dataframe 的条件在 dataframe 中创建新列? - How to create new column in dataframe based on condition from other dataframe? python pandas dataframe从其他列的单元格创建新列 - python pandas dataframe create new column from other columns' cells 如何通过引用其他两列在 Python Dataframe 中创建新列? - How to create a new column in Python Dataframe by referencing two other columns? 如何根据python中现有列中的条件创建新列? - How to create a new column based on conditions in the existing columns in a dataframe in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM