Skip to main content
 首页 » 编程设计

Python Pandas Dataframe之使用使用行值和列名的计算填充空列

2025年05月04日23cloudgamer

我有一个数据框 df,可以用它来创建:

import pandas as pd 
import datetime 
#create the dates to make into columns 
datestart=datetime.date(2018,1,1) 
dateend=datetime.date(2018,1,5) 
newcols=pd.date_range(datestart,dateend).date 
#create the test data 
d={'name':['a','b','c','d'],'earlydate': [datetime.date(2018,1,1),datetime.date(2018,1,3),datetime.date(2018,1,4),datetime.date(2018,1,5)]} 
#create initial test dataframe 
df=pd.DataFrame(data=d) 
#create the new dataframe with empty newcols 
df=pd.concat([df,pd.DataFrame(columns=newcols)]) 

看起来像这样:

df 
Out[17]:  
  name   earlydate 2018-01-01    ...     2018-01-03 2018-01-04 2018-01-05 
0    a  2018-01-01        NaN    ...            NaN        NaN        NaN 
1    b  2018-01-03        NaN    ...            NaN        NaN        NaN 
2    c  2018-01-04        NaN    ...            NaN        NaN        NaN 
3    d  2018-01-05        NaN    ...            NaN        NaN        NaN 
 
[4 rows x 7 columns] 

我想要做的是用 newcol 名称和 earlydate(newcolname(这是一个日期)-earlydate(这是一个日期)之间的天数差异填充所有空的 newcols。我想这样做数据框“明智”,而不是使用函数、lambda、应用或 for 循环。我相当确定这应该能够明智地完成数据框,而不是明智的列或行。

结果/预期的结束 df 可以用这个创建:

dresultdata={'name':['a','b','c','d'], 
         'earlydate': [datetime.date(2018,1,1),datetime.date(2018,1,3),datetime.date(2018,1,4),datetime.date(2018,1,5)], 
         datetime.date(2018,1,1):[0,-2,-3,-4], #this is the difference in days between the column name and the earlydate 
         datetime.date(2018,1,2):[-1,1,2,3], 
         datetime.date(2018,1,3):[-2,0,1,2], 
         datetime.date(2018,1,4):[-3,-1,0,1]} 
dferesult=pd.DataFrame(data=dresultdata) 

看起来像这样:

dferesult 
Out[19]:  
  name   earlydate  2018-01-01  2018-01-02  2018-01-03  2018-01-04 
0    a  2018-01-01           0          -1          -2          -3 
1    b  2018-01-03          -2           1           0          -1 
2    c  2018-01-04          -3           2           1           0 
3    d  2018-01-05          -4           3           2           1 

我通过如下循环完成了这项工作:

for d in newcols: 
    df.loc[:,d]=d-df.earlydate 

但是对于大帧(1m 行)它需要很长时间。欢迎提出想法!

请您参考如下方法:

IIUC:

i = pd.to_datetime(df.earlydate.values).values 
j = pd.to_datetime(df.columns[2:]).values 
 
df.iloc[:, 2:] = (j - i[:, None]).astype('timedelta64[D]').astype(int) 
 
df 
 
    earlydate name 2018-01-01 2018-01-02 2018-01-03 2018-01-04 2018-01-05 
0  2018-01-01    a          0          1          2          3          4 
1  2018-01-03    b         -2         -1          0          1          2 
2  2018-01-04    c         -3         -2         -1          0          1 
3  2018-01-05    d         -4         -3         -2         -1          0