简体   繁体   English

合并多个数据框列

[英]Combining multiple data-frame columns

I am trying to combine 2 data frames columns into 1 but when I try to do it based on specific size the second data-frame column doesn't copy correctly. 我正在尝试将2个数据帧列合并为1,但是当我尝试根据特定大小进行操作时,第二个数据帧列无法正确复制。

I have tried the code below as pasted below. 我已经尝试了下面粘贴下面的代码。

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()

File: month.csv 档案: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

File: year.csv 文件: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

These are the CURRENT results: 这些是当前结果:

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

Expected results are: 预期结果是:

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

Let's say you have an arbitrary number of dataframes dfA , dfB , dfC , etc. You want to merge them, but they're different sizes. 假设您有任意数量的数据帧dfAdfBdfC等。您想合并它们,但是它们的大小不同。 The most basic approach is to concatenate them: 最基本的方法是将它们串联起来:

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

But if the dataframes are different sizes, there will be missing rows. 但是,如果数据帧的大小不同,则会缺少行。 If you don't care which rows are preserved, you can just drop rows with missing values: 如果您不在乎保留哪些行,则可以删除缺少值的行:

df.dropna()

But if you specifically want to use the last N rows of each dataframe, where N is the length of the smallest dataframe, you need to do a little more work. 但是,如果您特别想使用每个数据帧的最后N行,其中N是最小数据帧的长度,则需要做更多的工作。 But I'll wait and see if that's what you want. 但是,我将等待,看看这是否是您想要的。


Old Answer: 旧答案:

Merging can be much simpler than this. 合并可能比这简单得多。 Use pd.merge : 使用pd.merge

pd.merge(dfY, dfM[-len(dfY):].reset_index(), 
    suffixes=['_y', '_m'], left_index=True, right_index=True)
  • dfM[-len(dfY):] gets the last N rows of dfM , where N is the length of dfY . dfM[-len(dfY):]得到的最后NdfM ,其中N是长度dfY
  • .reset_index() makes the index of the subset of dfM start at 0, so it can correctly align with dfY . .reset_index()使.reset_index()的子集的dfM从0开始,因此它可以与dfY正确对齐。
  • suffixes=['_y', '_m'] keeps the column names different. suffixes=['_y', '_m']使列名保持不同。 You can rename these if you like. 您可以根据需要重命名这些名称。

The problem was related to the index. 问题与索引有关。 If you run the code below: 如果您运行下面的代码:

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

You will get the output: 您将获得输出:

     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

The index is starting from 0 索引从0开始

And running this: 并运行此:

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

You will get the output: 您将获得输出:

    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

Here, the index is starting from 10 在这里,索引从10开始

So, you need to reset the index of the columns 'date' and 'Y_N' of dfM dataframe,like this: 因此,您需要重置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()

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM