简体   繁体   English

Python基于另一个变量(熊猫)在数据框中生成虚拟对象

[英]Python Generate dummy in dataframe based on another variable(pandas)

I have dataframe with many variables.我有很多变量的数据框。 I would like to generate a dummy variable based on column 1, for example.例如,我想根据第 1 列生成一个虚拟变量。 If column 1's observation is more than 0.25 then the dummy variable is filled with 1. If column 1' observation is less than 0.25, then the dummy variable is filled with 0. Any ideas?如果第 1 列的观察值大于 0.25,则虚拟变量填充为 1。如果第 1 列的观察值小于 0.25,则填充虚拟变量为 0。有什么想法吗? Thanks a lot.非常感谢。

IIUC compare values for greater like 0.25 and then cast to integers for map True/False to 1/0 : IIUC 比较大于0.25值,然后转换为整数以将True/False映射到1/0

df[1] = df[1].gt(0.25).astype(int)

If first column:如果第一列:

df.iloc[:, 0] = df.iloc[:, 0].gt(0.25).astype(int)

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

相关问题 Python 基于另一个变量在 dataframe 中生成 dummy - Python Generate dummy in dataframe based on another variable 基于另一个数据框生成熊猫数据框 - Generate pandas dataframe based on another dataframe python和pandas中是否有虚拟比较变量 - Is there a dummy comparison variable in python and pandas 为 dataframe 中的变量列表创建缺失的虚拟指标变量,并在 python 中使用特定值(熊猫) - Creating missing dummy indicator variable for a list of variable in a dataframe with specific values in python (pandas) Python Pandas groupby 基于另一个 dataframe - Python Pandas groupby based on another dataframe Pandas:用另一个表的“虚拟变量”创建一个表 - Pandas: Create a table with a “dummy variable” of another table 基于pandas python中分组的第n个十分位数的虚拟 - Dummy based on nth decile by group in pandas python 根据Python Pandas中另一个数据框中的数据选择一个数据框中的行 - Selecting rows in one dataframe based on data in another dataframe in Python Pandas 如何基于熊猫python中的另一个数据框获取数据框的子集 - How to get the subset of dataframe based on another dataframe in pandas python Python/Pandas:基于另一个 dataframe 过滤和组织 dataframe 的行和列 - Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM