Ada kerangka data. Kolom periode berisi daftar. Daftar ini berisi rentang waktu.

#load data
df = pd.DataFrame(data, columns=['task_id', 'target_start_date', 'target_end_date'])

df['target_start_date'] = pd.to_datetime(df.target_start_date)
df['target_end_date'] = pd.to_datetime(df.target_end_date)
df['period'] = np.nan


#create period column 
z = dict()
freq = 'M'

for i in range(0, len(df)):
    l = pd.period_range(df.target_start_date[i], df.target_end_date[i], freq=freq)
    l = l.to_native_types()
    z[i] = l

df.period = z.values()

Keluaran

    task_id  target_start_date  target_end_date     period

0   35851   2019-04-01 07:00:00 2019-04-01 07:00:00 [2019-04]
1   35852   2020-02-26 11:30:00 2020-02-26 11:30:00 [2020-02]
2   35854   2019-05-17 07:00:00 2019-06-01 17:30:00 [2019-05, 2019-06]
3   35855   2019-03-20 11:30:00 2019-04-07 15:00:00 [2019-03, 2019-04]
4   35856   2019-04-06 08:00:00 2019-04-26 19:00:00 [2019-04]

masukkan deskripsi gambar di sini

Lalu saya menambahkan kolom yang disebut irisan waktu.

#create slices
date_min = df.target_start_date.min()
date_max = df.target_end_date.max()
period = pd.period_range(date_min, date_max, freq=freq)

#add columns
for i in period:
    df[str(i)] = np.nan

Hasil

masukkan deskripsi gambar di sini

Bagaimana saya bisa mengisi nilai Nan untuk True, jika nilai ini ada dalam daftar di kolom periode?

masukkan deskripsi gambar di sini

0
Yury Ivanov 31 Mei 2019, 12:24

2 jawaban

Jawaban Terbaik

Pendekatan saya adalah mengulangi nama baris dan kolom dan membandingkan nilai:

import numpy as np
import pandas as pd

# handle assignment error
pd.options.mode.chained_assignment = None

# setup test data
data = {'time': [['2019-04'], ['2019-01'], ['2019-03'], ['2019-06', '2019-05']]}
data = pd.DataFrame(data=data)

# create periods
date_min = data.time.min()[0]
date_max = data.time.max()[0]
period = pd.period_range(date_min, date_max, freq='M')

for i in period:
    data[str(i)] = np.nan

# compare and fill data
for index, row in data.iterrows():
    for column in data:
        if data[column].name in row['time']:
            data[column][index] = 'True' 

Keluaran:

                 time 2019-01  2019-02 2019-03 2019-04 2019-05 2019-06
0           [2019-04]     NaN      NaN     NaN    True     NaN     NaN
1           [2019-01]    True      NaN     NaN     NaN     NaN     NaN
2           [2019-03]     NaN      NaN    True     NaN     NaN     NaN
3  [2019-06, 2019-05]     NaN      NaN     NaN     NaN    True    True
0
Zaraki Kenpachi 31 Mei 2019, 10:38

Terapkan fungsi di seluruh baris kerangka data

def fillit(row):
    for i in row.period:
        row[i] = True

df.apply(fillit), axis=1)
1
Will 31 Mei 2019, 09:33