Python,如何合并2个pandas DataFrame

标签 python pandas merge dataframe

假设我有两个具有不同索引的不同 pandas Dataframe 例如:

df1:

email                |       other_field
_________________________________________
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b4e464a42471a6b4e464a424705484446" rel="noreferrer noopener nofollow">[email protected]</a>     |           2
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc99919d9590cebc99919d9590d29f9391" rel="noreferrer noopener nofollow">[email protected]</a>     |           1
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ffaf2fef6f3acdffaf2fef6f3b1fcf0f2" rel="noreferrer noopener nofollow">[email protected]</a>     |           6

和 df2:

new_field
__________
    1
    7
    4

两个数据帧具有相同的大小。 我怎样才能合并它们两个以获得类似的输出?

df3:

email                |       other_field     |       new_field
________________________________________________________________
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95f0f8f4fcf9a4d5f0f8f4fcf9bbf6faf8" rel="noreferrer noopener nofollow">[email protected]</a>     |           2           |           1
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6e666a6267394b6e666a626725686466" rel="noreferrer noopener nofollow">[email protected]</a>     |           1           |           7
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aecbc3cfc7c29deecbc3cfc7c280cdc1c3" rel="noreferrer noopener nofollow">[email protected]</a>     |           6           |           4

我尝试过这个:

df3 = pd.merge(df1, df2, left_index=True, right_index=True)

但是尽管 df1 和 df2 具有相同的大小,但 df3 的大小较小

最佳答案

在这种情况下,您可以concat:

In [70]:

pd.concat([df1,df2],axis=1)

Out[70]:
              email  other_field  new_field
0  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="56333b373f3a6716333b373f3a7835393b" rel="noreferrer noopener nofollow">[email protected]</a>            2          1
1  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8a828e8683ddaf8a828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a>            1          7
2  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d5ddd1d9dc83f0d5ddd1d9dc9ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>            6          4

如果需要,您可以选择传递 ignore_index=True

join 也可以:

In [71]:

df1.join(df2)

Out[71]:
              email  other_field  new_field
0  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5b535f57520f7e5b535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>            2          1
1  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8ada5a9a1a4fa88ada5a9a1a4e6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a>            1          7
2  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a2aaa6aeabf487a2aaa6aeabe9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>            6          4

此外,在索引匹配的情况下,直接赋值也可以:

In [72]:

df1['new_field'] = df2['new_field']
df1
Out[72]:
              email  other_field  new_field
0  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="650008040c0954250008040c094b060a08" rel="noreferrer noopener nofollow">[email protected]</a>            2          1
1  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b0b8b4bcb9e795b0b8b4bcb9fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>            1          7
2  <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcb9b1bdb5b0ef9cb9b1bdb5b0f2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>            6          4

关于Python,如何合并2个pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385843/

相关文章:

python - Google Stackdriver 未按 Google Kubernetes Engine 的预期显示结构条目日志

python - 如何在开发中为 Django 提供 CSS?

python - 在 Pandas DataFrame 中用一张图定义两列

SQL合并两个结果集

python - 从表中垂直读取抓取的数据,而不是水平读取 Python

Python SOCKS5 代理客户端 HTTPS

pandas - 矢量化 pandas 申请 pd.date_range

python - 随机化两个 csv 文件,但索引顺序相同

Python:组合两个排序列表(并保持它们排序)而不使用内置排序

c++ - 傅里叶逆变换手动合并实部和虚部