Допустим, у меня есть два DataFrames, как показано ниже:
DF1:
from datetime import date, timedelta
import pandas as pd
import numpy as np
sdate = date(2019,1,1) # start date
edate = date(2019,1,7) # end date
required_dates = pd.date_range(sdate,edate-timedelta(days=1),freq='d')
# initialize list of lists
data = [['2019-01-01', 1001], ['2019-01-03', 1121] ,['2019-01-02', 1500],
['2019-01-02', 1400],['2019-01-04', 1501],['2019-01-01', 1200],
['2019-01-04', 1201],['2019-01-04', 1551],['2019-01-05', 1400]]
# Create the pandas DataFrame
df1 = pd.DataFrame(data, columns = ['OnlyDate', 'TBID'])
df1.sort_values(by='OnlyDate',inplace=True)
df1
OnlyDate TBID
0 2019-01-01 1001
5 2019-01-01 1200
2 2019-01-02 1500
3 2019-01-02 1400
1 2019-01-03 1121
4 2019-01-04 1501
6 2019-01-04 1201
7 2019-01-04 1551
8 2019-01-05 1400
DF2:
df2=pd.DataFrame(columns=[sorted(df1['TBID'].unique())],index=required_dates)
df2
1001 1121 1200 1201 1400 1500 1501 1551
2019-01-01 NaN NaN NaN NaN NaN NaN NaN NaN
2019-01-02 NaN NaN NaN NaN NaN NaN NaN NaN
2019-01-03 NaN NaN NaN NaN NaN NaN NaN NaN
2019-01-04 NaN NaN NaN NaN NaN NaN NaN NaN
2019-01-05 NaN NaN NaN NaN NaN NaN NaN NaN
2019-01-06 NaN NaN NaN NaN NaN NaN NaN NaN
Я пытаюсь применить (True или 1) к этому кадру данных DF3 по отношению к значениям из df1, как показано ниже:
df3 =df2.copy()
for index, row in df1.iterrows():
df3.loc[row['OnlyDate'],row['TBID']] = 1
df3.fillna(0, inplace=True)
df3
1001 1121 1200 1201 1400 1500 1501 1551
2019-01-01 1 0 1 0 0 0 0 0
2019-01-02 0 0 0 0 1 1 0 0
2019-01-03 0 1 0 0 0 0 0 0
2019-01-04 0 0 0 1 0 0 1 1
2019-01-05 0 0 0 0 1 0 0 0
2019-01-06 0 0 0 0 0 0 0 0
Есть ли лучший способ сделать это?