Python 如何将数据帧中的真假值转换为 1 表示真,0 表示假

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29960733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:13:37  来源:igfitidea点击:

How to convert true false values in dataframe as 1 for true and 0 for false

pythonpandasdataframeboolean

提问by Ashim Sinha

How to convert true false values in Dataframe as 1 for true and 0 for false

如何将Dataframe中的真假值转换为1为真,0为假

COL1  COL2 COL3  COL4
12   TRUE  14    FALSE
13   FALSE  13    TRUE


OUTPUT
12   1  14 0
13   0  13 1

回答by Zero

You could convert the type of each column like

您可以转换每列的类型,如

In [7]: df[['COL2', 'COL4']] = df[['COL2', 'COL4']].astype(int)

In [8]: df
Out[8]:
   COL1  COL2  COL3  COL4
0    12     1    14     0
1    13     0    13     1

Even df[['COL2', 'COL4']].astype(float)works for conversion.

甚至df[['COL2', 'COL4']].astype(float)适用于转换。

回答by abarnert

First, if you have the strings 'TRUE'and 'FALSE', you can convert those to boolean Trueand Falsevalues like this:

首先,如果您有字符串'TRUE'and 'FALSE',您可以将它们转换为布尔值TrueFalse值,如下所示:

df['COL2'] == 'TRUE'

That gives you a boolcolumn. You can use astypeto convert to int(because boolis an integral type, where Truemeans 1and Falsemeans 0, which is exactly what you want):

这给了你一个bool专栏。您可以使用astypeto 转换为int(因为bool是整数类型,其中Truemean1Falsemean 0,这正是您想要的):

(df['COL2'] == 'TRUE').astype(int)

To replace the old string column with this new intcolumn, just assign it:

要用这个新int列替换旧的字符串列,只需分配它:

df['COL2'] = (df['COL2'] == 'TRUE').astype(int)

And to do that to two columns at one, just index with a list of columns:

并且要对两列执行此操作,只需使用列列表进行索引:

df[['COL2', 'COL4']] = (df[['COL2', 'COL4']] == 'TRUE').astype(int)

回答by Guillem López Garcia

Simply doing this:

只需这样做:

df[["col2", "col4"]] *= 1

Python considers boolean values (True, False) like (1, 0) respectively. So you can operate with it like numbers.

Python 分别考虑像 (1, 0) 这样的布尔值 (True, False)。所以你可以像数字一样使用它。

回答by Naveen Reddy

You can also try this to convert the boolean values like True or False to 1 or 0.

您也可以尝试将 True 或 False 等布尔值转换为 1 或 0。

    In [2] : df['attribute_name']
    Out[2] : 0 True
             1 False
             2 True
             3 True

Now import these packages:

现在导入这些包:

    In [3] : from sklearn import preprocessing
             lab_enc = preprocessing.LabelEncoder()
             lab_enc.fit(df['attribute_name'])
             variable = lab_enc.transform(df['attribute_name'])
             df['variable'] = variable
             print df['variable']
    Out[4] : 0 1
             1 0
             2 1
             3 1

If you want to revert back the values from 0 or 1 to False or True you can use lab_encoder.inverse_transform([0,1]) which results the output from 0 or 1 to False or True

如果要将值从 0 或 1 恢复为 False 或 True,您可以使用 lab_encoder.inverse_transform([0,1]) 将输出从 0 或 1 恢复为 False 或 True

回答by Anon George

You can convert the 'True'and 'False'values (strings) to 1and 0respectively for a specific column(here we choose 3rd column) as follows.

您可以将特定(这里我们选择第 3 列)的'True''False'值(字符串)分别转换为10,如下所示。

from pandas import DataFrame as df
data = df(data) # where data contains your data as rows and columns
                # and it is converted to dataframe using pandas (ignore if already df)
for i in range(len(data[3])):
    if data[3][i] == 'TRUE':
        data[3][i] = 1
    elif data[3][i] == 'FALSE':
        data[3][i] = 0
    else:
        pass

This method can be used to compare any value or string and replace that location with the required value or string.

此方法可用于比较任何值或字符串并将该位置替换为所需的值或字符串。

回答by james

Suppose dis the dataframe you want to convert

假设d是您要转换的数据帧

f = lambda x: 1 if x==True else 0

d.applymap(f)should be what you want.

d.applymap(f)应该是你想要的。

回答by Hadi Rasekh

If you have a categorical column in your data (such as country name) .astype(int)will return an error A better choice is to multiply your data with one

如果您的数据中有分类列(例如国家/地区名称).astype(int)将返回错误更好的选择是将您的数据乘以 1

data = pd.read_csv('data.txt', header = None) 
data *= 1 # make true/false -> 1/0
print(data)

so if you have

所以如果你有

True False USA
False False USA
True True russia

result will be

结果将是

1 0 USA
0 0 USA
1 1 USA

回答by Saman

df=pd.DataFrame(data={'col1' : [True, False, True],
                 'col2': [14, 15, 12],
                 'col3': [False, True, True]})
df[['col1', 'col3']]=df[['col1', 'col3']].astype('int')
df
df=pd.DataFrame(data={'col1' : [True, False, True],
                 'col2': [14, 15, 12],
                 'col3': [False, True, True]})
df[['col1', 'col3']]=df[['col1', 'col3']].astype('int')
df
Output:
    col1    col2    col3
0   1        14      0
1   0        15      1
2   1        12      1

回答by CRAZYDATA

This does not work:

这不起作用:

df['COL2'] = (df['COL2'] == 'TRUE').astype(int)

This works:

这有效:

df['COL2'] = (df['COL2'] == True ).astype(int)

回答by Atharva Kousadikar

You can try following method:

您可以尝试以下方法:

variable_name = {'True' : 0 , 'False' : 1 }

data['Column_name'] = data['Column_name'].map(Variable_name)