1

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
1
  • what do you mean by more efficient here? is it to have a more parametric way to do it because your end goal is to duplicates df many times with a lot of different values in the two extra columns? Commented May 31, 2021 at 14:17

2 Answers 2

2

Try assign which creates a copy on-the-fly:

const_date = pd.Timestamp('2018-01-01')

out = pd.concat([df.assign(include_img=True, data_since=const_date),
                 df.assign(include_img=False, data_since=pd.NaT)],
                ignore_index=True)

Output:

   account_id type  include_img data_since
0           1    a         True 2018-01-01
1           2    b         True 2018-01-01
2           1    a        False        NaT
3           2    b        False        NaT
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a possible solution:

df = pd.concat([df, df], ignore_index=True)
df['include_images'] = False
df['data_since'] = np.nan
df.loc[range(len(df) // 2), ('include_images', 'data_since')] = (True, datetime(2018, 1, 1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.