I have a pandas DataFrame.
import pandas as pd
import numpy as np
from datetime import datetime
d = {'account_id': [1, 2], 'type': ['a', 'b']}
df = pd.DataFrame(data=d)
| account_id | type | |
|---|---|---|
| 0 | 1 | a |
| 1 | 2 | b |
I want to add two columns include_images and data_since which should contain one time True and a date for every row of the original DataFrame and one time False and NaN for every row of the original DataFrame.
Is there a more efficient way of writing this then like so:
df_a = df.copy()
df_a['include_images'] = True
df_a['data_since'] = datetime(2018, 1, 1)
df_b = df.copy()
df_b['include_images'] = False
df_b['data_since'] = np.nan
df = pd.concat([df_a, df_b], ignore_index=True)
| account_id | type | include_images | data_since | |
|---|---|---|---|---|
| 0 | 1 | a | True | 2018-01-01 00:00:00 |
| 1 | 2 | b | True | 2018-01-01 00:00:00 |
| 2 | 1 | a | False | NaT |
| 3 | 2 | b | False | NaT |