Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to specify default value when constructing Pandas Dataframe from two series (index and columns)?

I’m trying to construct a boolean 2D array set to initial value of False. The following code sets it to True by default:

import pandas as pd
from datetime import date

date_start = date(2022, 1, 1)
date_end = date(2022, 8, 24)
valid_dates = pd.bdate_range(date_start, date_end)
cols = range(0,4)
df = pd.DataFrame(index=valid_dates, columns=cols, dtype='bool')

I know I can do the following to replace the values to False, but it takes significantly longer:

df = df.replace(df, False)

My actual columns is much larger e.g. ~500 columns. Is there a way to just initialize the dataframe to be False?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import pandas as pd
import numpy as np 

def makefalse_numpy():
    return pd.DataFrame(np.full((500, 500), False))

%timeit makefalse_numpy

output:

10.8 ns ± 0.0466 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading