简体   繁体   English

从两个系列(索引和列)构造 Pandas Dataframe 时如何指定默认值?

[英]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.我正在尝试构建一个 boolean 二维数组设置为 False 的初始值。 The following code sets it to True by default:以下代码默认将其设置为 True:

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:我知道我可以执行以下操作来将值替换为 False,但这需要更长的时间:

df = df.replace(df, False)

My actual columns is much larger eg ~500 columns.我的实际列要大得多,例如~500 列。 Is there a way to just initialize the dataframe to be False?有没有办法将 dataframe 初始化为 False?

Thank You to @ivanp谢谢@ivanp

This is a working version that set the dataframe to false using my previous example and @ivanp's solution这是使用我之前的示例和@ivanp 的解决方案将 dataframe 设置为 false 的工作版本

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: output:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过xlwings将数据从pandas中的数据帧写入Excel时如何跳过默认索引(左侧的系列列)? - How to skip default index (series column on the left) when writing data from dataframe in pandas into Excel through xlwings? 如何将系列索引转换为两列作为DataFrame - How to convert Series index into two columns as a DataFrame 在熊猫数据框中设置两列作为索引以进行时间序列分析 - set two columns as the index in a pandas dataframe for time series analysis 从多索引熊猫数据框中引用熊猫系列值 - Refer to a pandas series value from a multi-index pandas dataframe 将系列索引中的值添加到Pandas DataFrame中等值的行 - Add value from series index to row of equal value in Pandas DataFrame 从满足某些条件的Pandas中的multindex数据帧构造时间序列 - Constructing time series from multindex dataframe meeting certain conditions in Pandas 如何根据两列过滤时间序列 pandas dataframe? - How to filter a time series pandas dataframe based on two columns? 如何将具有多个索引的 pandas.core.series.Series object 转换为 pandas ZC699575A5E8AFD9EZFBCA1A 填充的所有列? - How to convert pandas.core.series.Series object with Multiple index to a pandas Dataframe with all columns filled? 构建特定 dtype 的数据框时,pandas 是否具有默认填充值? - Does pandas have a default fill value when constructing a dataframe of a specific dtype? 如何通过构造简单的 pandas 系列来定义正确的索引? - How to define a correct index by constructing simple pandas Series?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM