合并两个pandas数据帧 - 导致空df

时间:2018-02-12 13:58:47

标签: pandas merge python-3.6

我正在尝试合并两只pandas df:

 Final_data
    sp_return  date  dj_return
0    0.000000  1927   0.306237
1    0.394938  1928   0.512283
2   -0.124227  1929  -0.176560
3   -0.235981  1930  -0.292411
4   -0.416280  1931  -0.478050
5   -0.054336  1932  -0.142564
6    0.454805  1933   0.654280
7   -0.073477  1934   0.025883
8    0.372718  1935   0.345200
9    0.260973  1936   0.230361
10  -0.402949  1937  -0.346893

以下内容:

    date         president_name party
0   1921      Warren G. Harding     R
1   1922      Warren G. Harding     R
2   1923        Calvin Coolidge     R
3   1924        Calvin Coolidge     R
4   1925        Calvin Coolidge     R
5   1926        Calvin Coolidge     R
6   1927        Calvin Coolidge     R
7   1928        Calvin Coolidge     R
8   1929         Herbert Hoover     R
9   1930         Herbert Hoover     R
10  1931         Herbert Hoover     R
11  1932         Herbert Hoover     R

使用以下代码:

final_data1 = presidents.merge(final_data,  on='date')

然而,我最后得到的是一个空的“final_data1”df。你有什么建议为什么会这样吗?

请告诉我这个问题。

2 个答案:

答案 0 :(得分:4)

合并之前,请确保数据类型相同。或者添加以下行:

#include "FreeRTOS_mock.hpp"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <vector>

#include "Algo.hpp"

extern freertos::FreeRTOSMock FreeRTOSMockObj;

/* A sensor measurement */
std::vector<int32_t> input1 { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
std::vector<int32_t> output1 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
/* Not very pretty adaptation function but it does the job */
std::vector<std::tuple<int32_t, int32_t>> genSet(std::vector<int32_t> a, std::vector<int32_t> b)
{
    uint32_t i(0);
    std::vector<std::tuple<int32_t int32_t>> vectorToReturn(a.size());
    for (i = 0 ; i < a.size(); i++)
    {
        vectorToReturn[i] = std::make_tuple(a[i], b[i]);
    }
    return vectorToReturn;
}

/** Define the Value-Parameterized Tests class */
class AlgoToTest: public ::testing::TestWithParam<std::tuple<int32_t, int32_t>>
{
public:
    /* SetUp() is run immediately before a test starts. */
    virtual void SetUp()
    {
        algo = new Algo::Algo();
    }

    /* TearDown() is invoked immediately after a test finishes. */
    virtual void TearDown()
    {
        delete algo;
    }
    Algo::Algorithm* algo = NULL;
};

/* The test-case used to loop on */
TEST_P(AlgoToTest, AlgoTestCase1)
{
    int32_t outputValue(0);
    outputValue = algo->run(std::get<0>(GetParam()), std::get<1>(GetParam()));
    ASSERT_EQ(outputValue, std::get<3>(GetParam()));
}

INSTANTIATE_TEST_CASE_P(AlgoTestRun1, AlgoToTest, ::testing::ValuesIn(genSet(input1, output1)));

答案 1 :(得分:0)

我的代码:

df = pd.DataFrame({'Q4':[2906, 1508,738,206,154], 'i':1000})
df2 = pd.DataFrame({'Q4':[2906, 1508,738,206,1], 'j':2000})
df3 = df.merge(df2, on='Q4', how='outer')
df3.head()


      Q4         i       j
0   2906    1000.0  2000.0
1   1508    1000.0  2000.0
2   738     1000.0  2000.0
3   206     1000.0  2000.0
4   154     1000.0  NaN
5   1          NaN  2000.0