[英]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.