簡體   English   中英

pandas 數據框將 INT64 列轉換為布爾值

[英]pandas data frame transform INT64 columns to boolean

數據幀 df 中的某些列 df.column 存儲為數據類型 int64。

這些值都是 1 或 0。

有沒有辦法用布爾值替換這些值?

df['column_name'] = df['column_name'].astype('bool')

例如:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.random_integers(0,1,size=5), 
                  columns=['foo'])
print(df)
#    foo
# 0    0
# 1    1
# 2    0
# 3    1
# 4    1

df['foo'] = df['foo'].astype('bool')
print(df)

產量

     foo
0  False
1   True
2  False
3   True
4   True

給定column_names列表,您可以使用以下方法將多列轉換為bool dtype:

df[column_names] = df[column_names].astype(bool)

如果您沒有列名列表,但希望轉換所有數字列,那么您可以使用

column_names = df.select_dtypes(include=[np.number]).columns
df[column_names] = df[column_names].astype(bool)

參考:Stack Overflow unutbu(1 月 9 日 13:25),BrenBarn(2017 年 9 月 18 日)

我有像年齡和 ID 這樣的數字列,我不想將它們轉換為布爾值。 因此,在確定像 unutbu 向我們展示的數字列之后,我過濾掉了最大值超過 1 的列。

# code as per unutbu
column_names = df.select_dtypes(include=[np.number]).columns 

# re-extracting the columns of numerical type (using awesome np.number1 :)) then getting the max of those and storing them in a temporary variable m.
m=df[df.select_dtypes(include=[np.number]).columns].max().reset_index(name='max')

# I then did a filter like BrenBarn showed in another post to extract the rows which had the max == 1 and stored it in a temporary variable n.
n=m.loc[m['max']==1, 'max']

# I then extracted the indexes of the rows from n and stored them in temporary variable p.
# These indexes are the same as the indexes from my original dataframe 'df'.
p=column_names[n.index]

# I then used the final piece of the code from unutbu calling the indexes of the rows which had the max == 1 as stored in my variable p.
# If I used column_names directly instead of p, all my numerical columns would turn into Booleans.
df[p] = df[p].astype(bool)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM