python - 合并非唯一列 - pandas python

标签 python python-3.x pandas join merge

我一直在尝试合并两个 DataFrames一起( dfdf_details )以类似于 Excel“vlookup”的方式但得到奇怪的结果。下面我展示了两个DataFrames的结构为简单起见,无需填充真实数据

df_details:

Abstract_Title  |  Abstract_URL  |  Session_No_v2  | Session_URL | Session_ID
  -------------------------------------------------------------------------
Abstract_Title1    Abstract_URL1         1          Session_URL1     12345
Abstract_Title2    Abstract_URL2         1          Session_URL1     12345
Abstract_Title3    Abstract_URL3         1          Session_URL1     12345
Abstract_Title4    Abstract_URL4         2          Session_URL2     22222 
Abstract_Title5    Abstract_URL5         2          Session_URL2     22222
Abstract_Title6    Abstract_URL6         3          Session_URL3     98765
Abstract_Title7    Abstract_URL7         3          Session_URL3     98765

df:

Session_Title   |   Session_URL   |   Sponsors   |    Type    |   Session_ID
    -------------------------------------------------------------------------------
Session_Title1     Session_URL1        x, y z     Paper             12345
Session_Title2     Session_URL2         x, y      Presentation      22222
Session_Title3     Session_URL3        a, b ,c    Presentation      98765
Session_Title4     Session_URL4          c        Talk              12121
Session_Title5     Session_URL5         a, x      Paper             33333

我想沿着 Session_ID 合并我想要最后的 DataFrame看起来像: enter image description here

我尝试了以下脚本,它产生了一个 DataFrame重复(多次)某些行并做奇怪的事情。例如,df_details有 7,046 行和 df有 1,856 行 - 当我运行以下合并代码时,我的 final_df结果为 21,148 行:

final_df = pd.merge(df_details, df, how = 'outer', on = 'Session_ID')

请帮忙!

最佳答案

为了生成最终输出表,我使用了以下代码:

final_df = pd.merge(df_details, df[['Session_ID',
                                'Session_Title',
                                'Sponsors',
                                'Type']], left_on = ['Session_ID'], right_on = ['Session_ID'], how = 'outer')

关于python - 合并非唯一列 - pandas python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312790/

相关文章:

python - 'pipenv install' 失败,TypeError : must be str, not type

存储日志的 Django Gunicorn

python - 通过使用并发 future 的多处理来运行多个视频

python - 在 Pandas 中创建一个列,用于计算另一列中唯一值的数量

python - Pandas,如何为所有以数字开头的列名附加前缀?

python - Python 运行最少需要哪些文件/库?

python - 相当于 Go 中的 Python string.format?

python - 异步: Why is awaiting a cancelled Future not showing CancelledError?

python - 获取数据框中特定数据点的行和列标签

python - 将输入数字转换为字母表示