合并多个数据框列

时间:2019-07-10 19:12:29

标签: python pandas dataframe

我试图将2个数据帧列合并为1,但是当我尝试根据特定大小进行操作时,第二个数据帧列无法正确复制。

我尝试了下面粘贴的以下代码。

import pandas as pd
def readDataFile():
    fileName = "year.csv"
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
    dfY = pd.read_csv(fileName, parse_dates=['date'], date_parser=dateparse)

    fileName = "month.csv"
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
    dfM = pd.read_csv(fileName, parse_dates=['date'], date_parser=dateparse)


    newDF = pd.DataFrame()
    newDF['date_y'] = dfY['date']
    newDF['year_y_n'] = dfY['Y_N']
    newDF['date_m'] = dfM['date'][len(dfM) - len(dfY):len(dfM)]
    newDF['year_y_n'] = dfM['Y_N'][len(dfM) - len(dfY):len(dfM)]
    print newDF
readDataFile()

文件:month.csv

date,Y_N
2018-03-14 04:00:00,N
2018-04-03 04:00:00,N
2018-05-31 04:00:00,Y
2018-06-14 04:00:00,N
2018-07-30 04:00:00,N
2018-08-31 04:00:00,Y
2018-09-28 04:00:00,N
2018-10-10 04:00:00,N
2018-11-07 04:00:00,Y
2018-12-31 04:00:00,N
2019-01-31 04:00:00,N
2019-02-05 04:00:00,Y
2019-03-29 04:00:00,N
2019-04-30 04:00:00,Y
2019-05-03 04:00:00,N
2019-06-03 04:00:00,Y

文件:year.csv

date,Y_N
2014-05-23 04:00:00,Y
2015-12-21 04:00:00,N
2016-05-03 04:00:00,Y
2017-12-20 04:00:00,N
2018-06-14 04:00:00,N
2019-06-25 04:00:00,N

以下是当前结果:

date_y year_y_n date_m month_y_n
0 2014-05-23 04:00:00        Y    NaT       NaN
1 2015-12-21 04:00:00        N    NaT       NaN
2 2016-05-03 04:00:00        Y    NaT       NaN
3 2017-12-20 04:00:00        N    NaT       NaN
4 2018-06-14 04:00:00        N    NaT       NaN
5 2019-06-25 04:00:00        N    NaT       NaN

预期结果是:

date_y              year_y_n    date_m              month_y_n
2014-05-23 04:00:00        Y  2019-01-31 04:00:00       N
2015-12-21 04:00:00        N  2019-02-05 04:00:00       Y
2016-05-03 04:00:00        Y  2019-03-29 04:00:00       N
2017-12-20 04:00:00        N  2019-04-30 04:00:00       Y
2018-06-14 04:00:00        N  2019-05-03 04:00:00       N
2019-06-25 04:00:00        N  2019-06-03 04:00:00       Y

2 个答案:

答案 0 :(得分:0)

假设您有任意数量的数据帧dfAdfBdfC等。您想合并它们,但是它们的大小不同。最基本的方法是将它们连接起来:

df = pd.concat([dfA, dfB, dfC], axis=1)

但是,如果数据帧的大小不同,则会丢失行。如果您不关心保留哪些行,则可以删除缺少值的行:

df.dropna()

但是,如果您特别想使用每个数据帧的最后 N 行,其中 N 是最小数据帧的长度,则您需要做更多的工作。但是我会等一下,看看那是不是你想要的。


旧答案:

合并可能比这简单得多。使用pd.merge

pd.merge(dfY, dfM[-len(dfY):].reset_index(), 
    suffixes=['_y', '_m'], left_index=True, right_index=True)
  • dfM[-len(dfY):]获取dfM的最后 N 行,其中 N dfY的长度。
  • .reset_index()使dfM的子集的索引从0开始,因此它可以正确地与dfY对齐。
  • suffixes=['_y', '_m']使列名保持不同。您可以根据需要将其重命名。

答案 1 :(得分:0)

问题与索引有关。 如果您运行以下代码:

newDF = pd.DataFrame()
newDF['date_y'] = dfY['date']
print(newDF)

您将获得输出:

     date_y
0 2014-05-23 04:00:00
1 2015-12-21 04:00:00
2 2016-05-03 04:00:00
3 2017-12-20 04:00:00
4 2018-06-14 04:00:00
5 2019-06-25 04:00:00

索引从0开始

运行此:

newDF = pd.DataFrame()
newDF['date_m'] = dfM['date'][len(dfM) - len(dfY):len(dfM)]
print(newDF)

您将获得输出:

    date_m
10 2019-01-31 04:00:00
11 2019-02-05 04:00:00
12 2019-03-29 04:00:00
13 2019-04-30 04:00:00
14 2019-05-03 04:00:00
15 2019-06-03 04:00:00

此处,索引从10开始

因此,您需要重置dfM数据帧的“日期”和“ Y_N”列的索引,如下所示:

def readDataFile():
    fileName = "year.csv"
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
    dfY = pd.read_csv(fileName, parse_dates=['date'], date_parser=dateparse)

    fileName = "month.csv"
    dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')
    dfM = pd.read_csv(fileName, parse_dates=['date'], date_parser=dateparse)


    newDF = pd.DataFrame()
    newDF['date_y'] = dfY['date']
    newDF['year_y_n'] = dfY['Y_N']

    # Changes made on this line.
    newDF['date_m'] = dfM['date'][len(dfM) - len(dfY):len(dfM)].reset_index(drop=True)
    newDF['month_y_n'] = dfM['Y_N'][len(dfM) - len(dfY):len(dfM)].reset_index(drop=True)

    print(newDF)
readDataFile()

输出:

date_y year_y_n              date_m month_y_n
0 2014-05-23 04:00:00        Y 2019-01-31 04:00:00         N
1 2015-12-21 04:00:00        N 2019-02-05 04:00:00         Y
2 2016-05-03 04:00:00        Y 2019-03-29 04:00:00         N
3 2017-12-20 04:00:00        N 2019-04-30 04:00:00         Y
4 2018-06-14 04:00:00        N 2019-05-03 04:00:00         N
5 2019-06-25 04:00:00        N 2019-06-03 04:00:00         Y