简体   繁体   中英

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 eg ~500 columns. Is there a way to just initialize the dataframe to be False?

Thank You to @ivanp

This is a working version that set the dataframe to false using my previous example and @ivanp's solution

import pandas as pd
import numpy as np 
from datetime import date

date_start = date(2022, 1, 2)
date_end = date(2022, 8, 24)
valid_dates = pd.bdate_range(date_start, date_end)
cols = range(0, 500)
df = pd.DataFrame(data = np.full((len(valid_dates), len(cols)), False), index=valid_dates, columns=cols)
print(df)
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM