2

我正在寻找与 pandas to_numeric() 等效的布尔值,如果可能的话,我希望该函数将列转换为 True/False/nan,如果没有则抛出错误。

我的动机是我需要自动识别和转换具有约 1000 列的数据集中的布尔列。我可以使用以下代码对浮点数/整数做类似的事情:

df = df_raw.apply(pd.to_numeric, errors='ignore')
4

4 回答 4

6

由于pd.to_numeric主要用于将字符串转换为数值,因此我将在您想要转换文字布尔值字符串的假设下工作。

考虑数据框df

df = pd.DataFrame([
        ['1', None, 'True'],
        ['False', 2, True]
    ])

print(df)

       0    1     2
0      1  NaN  True
1  False  2.0  True

我的选择
这就是我的建议。在下面,我将其分解以试图解释发生了什么。

def try_eval2(x):
    if type(x) is str:
        try:
            x = literal_eval(x)
        except:
            x = np.nan

    if type(x) is not bool:
        x = np.nan

    return x

vals = df.values
v = vals.ravel()
a = np.array([try_eval2(x) for x in v.tolist()], dtype=object)
pd.DataFrame(a.reshape(vals.shape), df.index, df.columns)

       0    1     2
0    NaN  NaN  True
1  False  NaN  True

时间
你会注意到我提出的解决方案非常快

%%timeit
vals = df.values
v = vals.ravel()
a = np.array([try_eval2(x) for x in v.tolist()], dtype=object)
pd.DataFrame(a.reshape(vals.shape), df.index, df.columns)
10000 loops, best of 3: 149 µs per loop

%timeit df.astype(str).applymap(to_boolean)
1000 loops, best of 3: 1.28 ms per loop

%timeit df.astype(str).stack().map({'True':True, 'False':False}).unstack()
1000 loops, best of 3: 1.27 ms per loop

解释

第 1 步
现在我将创建一个简单的函数,ast.literal_eval用于将字符串转换为值

from ast import literal_eval

def try_eval(x):
    try:
        x = literal_eval(x)
    except:
        pass
    return x

第 2 步
applymap使用我的新功能。它看起来会一样!

d1 = df.applymap(try_eval)
print(d1)

       0    1     2
0      1  NaN  True
1  False  2.0  True

步骤 3
使用whereandapplymap再次查找值的实际位置bool

d2 = d1.where(d1.applymap(type).eq(bool))
print(d2)

       0   1     2
0    NaN NaN  True
1  False NaN  True

第 4 步
您可以删除所有列NaN

print(d2.dropna(1, 'all'))

       0     2
0    NaN  True
1  False  True
于 2017-05-04T18:14:10.047 回答
5

你需要replacewherewhere replace to NaNall not boolean

df = df.replace({'True':True,'False':False})
df = df.where(df.applymap(type) == bool)

旧解决方案(非常慢):

astype如果 in 有一些布尔值,df您可以applymap使用自定义函数和ast.literal_eval转换为字符串:

from ast import literal_eval

def to_boolean(x):
    try:
        x = literal_eval(x)
        if type(x) == bool:
            return x
        else:
            return np.nan
    except:
        x = np.nan
    return x

print (df.astype(str).applymap(to_boolean))
#with borrowing sample from piRSquared
       0   1     2
0    NaN NaN  True
1  False NaN  True

时间

In [76]: %timeit (jez(df))
1 loop, best of 3: 488 ms per loop

In [77]: %timeit (jez2(df))
1 loop, best of 3: 527 ms per loop

#piRSquared fastest solution
In [78]: %timeit (pir(df))
1 loop, best of 3: 5.42 s per loop

#maxu solution
In [79]: %timeit df.astype(str).stack().map({'True':True, 'False':False}).unstack()
1 loop, best of 3: 1.88 s per loop

#jezrael ols solution
In [80]: %timeit df.astype(str).applymap(to_boolean)
1 loop, best of 3: 13.3 s per loop

计时码

df = pd.DataFrame([
        ['True', False, '1', 0, None, 5.2],
        ['False', True, '0', 1, 's', np.nan]])

#[20000 rows x 60 columns]
df = pd.concat([df]*10000).reset_index(drop=True)
df = pd.concat([df]*10, axis=1).reset_index(drop=True)
df.columns = pd.RangeIndex(len(df.columns))
#print (df)

def to_boolean(x):
    try:
        x = literal_eval(x)
        if type(x) == bool:
            return x
        else:
            return np.nan
    except:
        x = np.nan
    return x


def try_eval2(x):
    if type(x) is str:
        try:
            x = literal_eval(x)
        except:
            x = np.nan

    if type(x) is not bool:
        x = np.nan

    return x

def pir(df):
    vals = df.values
    v = vals.ravel()
    a = np.array([try_eval2(x) for x in v.tolist()], dtype=object)
    df2 = pd.DataFrame(a.reshape(vals.shape), df.index, df.columns)
    return (df2)

def jez(df):
    df = df.replace({'True':True,'False':False})
    df = df.where(df.applymap(type) == bool)
    return (df)

def jez2(df):
    df = df.replace({'True':True,'False':False})
    df = df.where(df.applymap(type).eq(bool))
    return (df)
于 2017-05-04T18:38:32.353 回答
3

我使用了@piRSquared 的示例 DF:

In [39]: df
Out[39]:
       0    1     2
0      1  NaN  True
1  False  2.0  True

In [40]: df.astype(str).stack().map({'True':True, 'False':False}).unstack()
Out[40]:
       0    1     2
0    NaN  NaN  True
1  False  NaN  True
于 2017-05-04T19:08:02.027 回答
3

astype是更具体的版本pd.to_numeric

df = df_raw.astype('bool') 
于 2017-05-04T17:52:40.357 回答