合并两个数据帧

时间:2021-07-17 19:33:23

标签: python pandas dataframe

我尝试合并两个数据框,但似乎无法让它工作。每次合并时,我期望值的行都是 0。数据框 df1 已经作为其中的一些数据,有些留空。数据框 df2 将填充 df1 中的那些空白行,其中列名称与“TempBin”中的每个值以及 df1 中“Month”中的每个值匹配。

编辑: 两个数据帧都在 for 循环中。 df1 充当我的“存储”,每次位置迭代都会更改 df2。因此,如果 df2 包含 LocationZP 的结果,我还希望将该数据插入到匹配的 df1 行中。如果我在 df1 = df1.append(df2) 循环中使用 for,则 df2 中的所有行都会在每次迭代的 df1 末尾插入。

df1:

Month  TempBin  LocationAA   LocationXA   LocationZP
 1      0       7            1            2
 1      1       98           0            89
 1      2       12           23           38
 1      3       3            14           17
 1      4       7            9            14
 1      5       1            8            99
 13     0       0            0            0
 13     1       0            0            0
 13     2       0            0            0
 13     3       0            0            0
 13     4       0            0            0
 13     5       0            0            0

df2:

Month  TempBin  LocationAA
 13     0       11
 13     1       22
 13     2       33
 13     3       44
 13     4       55
 13     5       66 

期望的 df1 输出:

Month  TempBin  LocationAA   LocationXA   LocationZP
 1      0       7            1            2
 1      1       98           0            89
 1      2       12           23           38
 1      3       3            14           17
 1      4       7            9            14
 1      5       1            8            99
 13     0       11           0            0
 13     1       22           0            0
 13     2       33           0            0
 13     3       44           0            0
 13     4       55           0            0
 13     5       66           0            0
import pandas as pd

df1 = pd.DataFrame({'Month': [1]*6 + [13]*6,
                   'TempBin': [0,1,2,3,4,5]*2,
                   'LocationAA': [7,98,12,3,7,1,0,0,0,0,0,0],
                   'LocationXA': [1,0,23,14,9,8,0,0,0,0,0,0],
                   'LocationZP': [2,89,38,17,14,99,0,0,0,0,0,0]}
                   )

df2 = pd.DataFrame({'Month': [13]*6,
                   'TempBin': [0,1,2,3,4,5],
                   'LocationAA': [11,22,33,44,55,66]}
                   )

df1 = pd.merge(df1, df2, on=["Month","TempBin","LocationAA"], how="left")

结果:

Month  TempBin  LocationAA  LocationXA  LocationZP
1      0        7.0         1.0         2.0
1      1        98.0        0.0         89.0
1      2        12.0        23.0        38.0
1      3        3.0         14.0        17.0
1      4        7.0         9.0         14.0
1      5        1.0         8.0         99.0
13     0        NaN         NaN         NaN
13     1        NaN         NaN         NaN
13     2        NaN         NaN         NaN
13     3        NaN         NaN         NaN
13     4        NaN         NaN         NaN
13     5        NaN         NaN         NaN

3 个答案:

答案 0 :(得分:1)

以下是一些对我有用的代码:

                <a href="{@link}" target="_blank">
                    <xsl:value-of select="."/>
                </a>

输出:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/source">
    <html>
        <body>
            <xsl:apply-templates select="content/body/div"/>
        </body>
    </html>
</xsl:template>

<xsl:template match="div">
    <p>
        <xsl:apply-templates select="text"/>
    </p>
</xsl:template>

<xsl:template match="place">
    <a href="{@link}" target="_blank">
        <xsl:value-of select="."/>
    </a>    
</xsl:template>

</xsl:stylesheet>

如果有什么不明白的地方请在评论中告诉我:)

PS:对于额外的评论,我们深表歉意。但我把它们留在那里以作更多解释。

答案 1 :(得分:0)

您需要使用 append 来获得所需的输出:

<div class="flex-container">
   <div>
      <p><strong>Transkription:</strong></p>
      <p>
              Wir Vorsteher und gesamte
         Meister des ehrsamen Handwerks der b&uuml;rgerl:[ichen] Tischlern in der K:[aiserlich]
              K:[&ouml;niglichen] Haubt = und Residenz Stadt <a href="http://whgazetteer.org/places/12346175/portal" target="_blank">Wien</a> (beglaubigen) hiermit,
              da&szlig; gegenwertiger Tischlergesell, Namens Georg
              Gramer von <a href="http://whgazetteer.org/places/13067462/portal" target="_blank">Maintz</a> - -
[etc.]
</p>
</div>
</div>

如果要将 Null 替换为零,请添加:

df1 = df1.append(df2)

答案 2 :(得分:0)

这是另一种使用 combine_first()

i = ['Month','TempBin']
df2.set_index(i).combine_first(df1.set_index(i)).reset_index()