创建基于 NaN 映射的 1 和 0 的键列
Create key column of 1s & 0s mapped based on NaN
我有一个 DataFrame
如下所示:
df
A B C D E
0 test NaN 10.0 a a
1 test NaN 10.0 a a
2 test x NaN a NaN
3 test NaN 12.0 NaN NaN
4 test x NaN NaN NaN
5 test NaN 14.0 g c
我想创建一个 key
列:
- 是一个字符串,长度==
len(df.columns)
- 每个
0
代表NaN
,每个1
代表不NaN
A B C D E key
0 test NaN 10.0 a a 10111
1 test NaN 10.0 a a 10111
2 test x NaN a NaN 11010
3 test NaN 12.0 NaN NaN 10100
4 test x NaN NaN NaN 11000
5 test NaN 14.0 g c 10111
我知道如何检查它,但不知道如何将它放入 1 列的 1 个字符串中。我当前的代码是:
for col in df.columns:
... print(df[col].isnull().astype(int).replace({1: 0, 0: 1}))
...
0 1
1 1
2 1
3 1
4 1
5 1
Name: A, dtype: int64
0 0
1 0
2 1
3 0
4 1
5 0
Name: B, dtype: int64
0 1
1 1
2 0
3 1
4 0
5 1
Name: C, dtype: int64
0 1
1 1
2 1
3 0
4 0
5 1
Name: D, dtype: int64
0 1
1 1
2 0
3 0
4 0
5 1
Name: E, dtype: int64
0 1
1 1
2 1
3 1
4 1
5 1
Name: key, dtype: int64
使用DataFrame.notna
with DataFrame.astype
and DataFrame.apply
:
df['key']=df.notna().astype(int).astype(str).apply(''.join,axis = 1)
#df['key']=df.notnull().astype(int).astype(str).apply(''.join,axis = 1)
print(df)
A B C D E key
0 test NaN 10.0 a a 10111
1 test NaN 10.0 a a 10111
2 test x NaN a NaN 11010
3 test NaN 12.0 NaN NaN 10100
4 test x NaN NaN NaN 11000
5 test NaN 14.0 g c 10111
另一种方法:DataFrame.replace
df['key'] = df.notna().replace({True:'1',False:'0'}).apply(''.join,1)
或
df['key'] = df.notna().astype(int).astype(str).stack().groupby(level=0).agg(''.join)
使用 numpy 和列表理解
df['key'] = [''.join(x) for x in np.where(df.isnull(),'0','1')]
print(df)
A B C D E key
0 test NaN 10.0 a a 10111
1 test NaN 10.0 a a 10111
2 test x NaN a NaN 11010
3 test NaN 12.0 NaN NaN 10100
4 test x NaN NaN NaN 11000
5 test NaN 14.0 g c 10111
我有一个 DataFrame
如下所示:
df
A B C D E
0 test NaN 10.0 a a
1 test NaN 10.0 a a
2 test x NaN a NaN
3 test NaN 12.0 NaN NaN
4 test x NaN NaN NaN
5 test NaN 14.0 g c
我想创建一个 key
列:
- 是一个字符串,长度==
len(df.columns)
- 每个
0
代表NaN
,每个1
代表不NaN
A B C D E key
0 test NaN 10.0 a a 10111
1 test NaN 10.0 a a 10111
2 test x NaN a NaN 11010
3 test NaN 12.0 NaN NaN 10100
4 test x NaN NaN NaN 11000
5 test NaN 14.0 g c 10111
我知道如何检查它,但不知道如何将它放入 1 列的 1 个字符串中。我当前的代码是:
for col in df.columns:
... print(df[col].isnull().astype(int).replace({1: 0, 0: 1}))
...
0 1
1 1
2 1
3 1
4 1
5 1
Name: A, dtype: int64
0 0
1 0
2 1
3 0
4 1
5 0
Name: B, dtype: int64
0 1
1 1
2 0
3 1
4 0
5 1
Name: C, dtype: int64
0 1
1 1
2 1
3 0
4 0
5 1
Name: D, dtype: int64
0 1
1 1
2 0
3 0
4 0
5 1
Name: E, dtype: int64
0 1
1 1
2 1
3 1
4 1
5 1
Name: key, dtype: int64
使用DataFrame.notna
with DataFrame.astype
and DataFrame.apply
:
df['key']=df.notna().astype(int).astype(str).apply(''.join,axis = 1)
#df['key']=df.notnull().astype(int).astype(str).apply(''.join,axis = 1)
print(df)
A B C D E key
0 test NaN 10.0 a a 10111
1 test NaN 10.0 a a 10111
2 test x NaN a NaN 11010
3 test NaN 12.0 NaN NaN 10100
4 test x NaN NaN NaN 11000
5 test NaN 14.0 g c 10111
另一种方法:DataFrame.replace
df['key'] = df.notna().replace({True:'1',False:'0'}).apply(''.join,1)
或
df['key'] = df.notna().astype(int).astype(str).stack().groupby(level=0).agg(''.join)
使用 numpy 和列表理解
df['key'] = [''.join(x) for x in np.where(df.isnull(),'0','1')]
print(df)
A B C D E key
0 test NaN 10.0 a a 10111
1 test NaN 10.0 a a 10111
2 test x NaN a NaN 11010
3 test NaN 12.0 NaN NaN 10100
4 test x NaN NaN NaN 11000
5 test NaN 14.0 g c 10111