Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将一行中的所有列追加到另一行中_Python_Pandas - Fatal编程技术网

Python 将一行中的所有列追加到另一行中

Python 将一行中的所有列追加到另一行中,python,pandas,Python,Pandas,我正在尝试将一行中的每一列追加到另一行中,我希望对每一行执行此操作,但某些行将没有任何值,请查看我的代码,这将更加清楚: 这是我的数据 date day_of_week day_of_month day_of_year month_of_year 5/1/2017 0 1 121 5 5/2/2017 1 2 122 5 5/3/2017 2 3 123 5 5/4/2017 3 4 124 5 5/8/2017 0

我正在尝试将一行中的每一列追加到另一行中,我希望对每一行执行此操作,但某些行将没有任何值,请查看我的代码,这将更加清楚:

这是我的数据

date    day_of_week day_of_month    day_of_year month_of_year
5/1/2017    0   1   121 5
5/2/2017    1   2   122 5
5/3/2017    2   3   123 5
5/4/2017    3   4   124 5
5/8/2017    0   8   128 5
5/9/2017    1   9   129 5
5/10/2017   2   10  130 5
5/11/2017   3   11  131 5
5/12/2017   4   12  132 5
5/15/2017   0   15  135 5
5/16/2017   1   16  136 5
5/17/2017   2   17  137 5
5/18/2017   3   18  138 5
5/19/2017   4   19  139 5
5/23/2017   1   23  143 5
5/24/2017   2   24  144 5
5/25/2017   3   25  145 5
5/26/2017   4   26  146 5
这是我目前的代码:

s = df_md['date'].shift(-1)
df_md['next_calendarday']  = s.mask(s.dt.dayofweek.diff().lt(0))
df_md.set_index('date', inplace=True)
df_md.apply(lambda row: GetNextDayMarketData(row, df_md), axis=1)

def GetNextDayMarketData(row, dataframe):
    if(row['next_calendarday'] is pd.NaT):
        return
    key = row['next_calendarday'].strftime("%Y-%m-%d")
    nextrow = dataframe.loc[key]
    for index, val in nextrow.iteritems():
        if(index != "next_calendarday"):
            dataframe.loc[row.name, index+'_nextday'] = val
这是可行的,但速度太慢了,还不如不可行。结果应该是这样的,您可以看到下一行的值已添加到上一行。关键在于它是下一个日历日期,而不仅仅是序列中的下一行。如果一行没有下一个日历日期的条目,则该行将为空

以下是csv中的预期结果

date    day_of_week day_of_month    day_of_year month_of_year   next_workingday day_of_week_nextday day_of_month_nextday    day_of_year_nextday month_of_year_nextday
5/1/2017    0   1   121 5   5/2/2017    1   2   122 5
5/2/2017    1   2   122 5   5/3/2017    2   3   123 5
5/3/2017    2   3   123 5   5/4/2017    3   4   124 5
5/4/2017    3   4   124 5                   
5/8/2017    0   8   128 5   5/9/2017    1   9   129 5
5/9/2017    1   9   129 5   5/10/2017   2   10  130 5
5/10/2017   2   10  130 5   5/11/2017   3   11  131 5
5/11/2017   3   11  131 5   5/12/2017   4   12  132 5
5/12/2017   4   12  132 5                   
5/15/2017   0   15  135 5   5/16/2017   1   16  136 5
5/16/2017   1   16  136 5   5/17/2017   2   17  137 5
5/17/2017   2   17  137 5   5/18/2017   3   18  138 5
5/18/2017   3   18  138 5   5/19/2017   4   19  139 5
5/19/2017   4   19  139 5                   
5/23/2017   1   23  143 5   5/24/2017   2   24  144 5
5/24/2017   2   24  144 5   5/25/2017   3   25  145 5
5/25/2017   3   25  145 5   5/26/2017   4   26  146 5
5/26/2017   4   26  146 5                   
5/30/2017   1   30  150 5   

            
与删除列一起使用下一个日历日下一天:

df = df.set_index('date')
df = (df.join(df, on='next_calendarday', rsuffix='_nextday')
        .drop('next_calendarday_nextday', axis=1))