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

痴心易碎 提交于 2020-12-24 06:47:59

问题


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

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


OUTPUT
12   1  14 0
13   0  13 1

回答1:


First, if you have the strings 'TRUE' and 'FALSE', you can convert those to boolean True and False values like this:

df['COL2'] == 'TRUE'

That gives you a bool column. You can use astype to convert to int (because bool is an integral type, where True means 1 and False means 0, which is exactly what you want):

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

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

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)



回答2:


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.




回答3:


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.




回答4:


Suppose d is the dataframe you want to convert

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

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




回答5:


This does not work:

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

This works:

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



回答6:


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



回答7:


You can try following method:

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

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



回答8:


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

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



回答9:


You can also try this to convert the boolean values like True or False to 1 or 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




回答10:


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

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.




回答11:


A simple and clean way to do this is using numpy's where function

df['Y'] = np.where(df['X'] == "SOME_VALUE", 1, 0)

will internally calculate True/False for values and replace them with 1/0 creating a array of ones and zeros.

Similarly,

df['Y'] = np.where((df['X'] == "SOME_VALUE") & (df['Z'] == "SOME_VALUE"), 100, 0)

works like a charm and is quite similar to Ms Excel calculations making it intuitive.




回答12:


Use pandas.DataFrame.replace

>>> df

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

>>> df.replace(['TRUE','FALSE'],[1,0])

   COL1  COL2  COL3  COL4
0    12     1    14     0
1    13     0    13     1


来源:https://stackoverflow.com/questions/29960733/how-to-convert-true-false-values-in-dataframe-as-1-for-true-and-0-for-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!