Python 比较并联接两个数据帧中的两列
我有两个具有相同列类型的数据帧 第一个数据帧(df1) 第二数据帧(df2) 预期输出(应删除FTM和LRC) 我尝试过的Python 比较并联接两个数据帧中的两列,python,dataframe,Python,Dataframe,我有两个具有相同列类型的数据帧 第一个数据帧(df1) 第二数据帧(df2) 预期输出(应删除FTM和LRC) 我尝试过的 Coin Quantity selling rate BTC 2 50000 ETH 1 1500 ADA 100 20 ... ... ... df1.merge(df2, how='outer',
Coin Quantity selling rate
BTC 2 50000
ETH 1 1500
ADA 100 20
... ... ...
df1.merge(df2, how='outer', left_on=['Coin'], right_on=['code_name'])
两个代码都没有给我预期的输出。我搜索了StackOverflow,没有找到任何有用的答案。如果存在相关问题,是否有人能给出解决方案或将此问题复制?您需要的是内部连接,而不是外部连接。内部联接只保留要联接在一起的两个表中常见的记录
import pandas as pd
# Make the first data frame
df1 = pd.DataFrame({
'Coin': ['BTC', 'ETH', 'ADA'],
'Quantity': [2, 1, 100]
})
# Make the second data frame
df2 = pd.DataFrame({
'code_name': ['BTC', 'FTM', 'ETH', 'LRC', 'ADA'],
'selling_rate': [50000, 50, 1500, 5, 20]
})
# Merge the data frames via inner join. This only keeps entries that appear in
# both data frames
full_df = df1.merge(df2, how = 'inner', left_on = 'Coin', right_on = 'code_name')
# Drop the duplicate column
full_df = full_df.drop('code_name', axis = 1)
因为merge()
对于大型数据集来说速度很慢。我宁愿不使用它,只要我有一个更快的解决方案。因此,我建议如下:
full_df = df1.copy()
full_df['selling_rate'] = list(
df2['selling_rate'][df2['code_name'].isin(df1['Coin'].unique())])
注意:如果
df1
和df2
与Coin
和code\u name
的顺序相同,则这将变成预期的解决方案。如果不是,您应该在上面的代码之前使用sort\u values()
。也许这只是您的示例中的一个输入错误,但是code\u name
在“我尝试过的”部分中是小写的,但是code\u name
在您对df2
的描述中不是完全小写的。对不起,code\u name
是简单的字母。我对它进行了编辑,只是想弄清楚,您的预期输出是否有df2
中没有出现在df1
中的行?换句话说,LRC
和FTM
都应该包含在您的预期输出中吗?不,我想从最终结果中删除不相等的行好的建议。我以前没有考虑<代码> SotTyValues()/<代码>。1.
Coin Quantity selling rate
BTC 2 50000
ETH 1 1500
ADA 100 20
... ... ...
df1.merge(df2, how='outer', left_on=['Coin'], right_on=['code_name'])
df = np.where(df1['Coin'] == df2['code_name'])
import pandas as pd
# Make the first data frame
df1 = pd.DataFrame({
'Coin': ['BTC', 'ETH', 'ADA'],
'Quantity': [2, 1, 100]
})
# Make the second data frame
df2 = pd.DataFrame({
'code_name': ['BTC', 'FTM', 'ETH', 'LRC', 'ADA'],
'selling_rate': [50000, 50, 1500, 5, 20]
})
# Merge the data frames via inner join. This only keeps entries that appear in
# both data frames
full_df = df1.merge(df2, how = 'inner', left_on = 'Coin', right_on = 'code_name')
# Drop the duplicate column
full_df = full_df.drop('code_name', axis = 1)
full_df = df1.copy()
full_df['selling_rate'] = list(
df2['selling_rate'][df2['code_name'].isin(df1['Coin'].unique())])