[英]DataFrame: Setting all the values to a particular columns as NaN
I have a DF like我有一个像
A B C D E F G H I
=====================================
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
I need to set all the values for columns except for A and C to be Null我需要将除 A 和 C 之外的列的所有值设置为 Null
A B C D E F G H I
=====================================
1 3
1 3
1 3
If there a way to do this instead of using the below code like using a not condition to check for only columns A & C and set null to every other columns如果有办法做到这一点,而不是使用下面的代码,比如使用非条件仅检查列 A 和 C 并将 null 设置为所有其他列
df[['B','D','E','F','G','H','I']] = '' df[['B','D','E','F','G','H','I']] = ''
An easy way could be to slice and reindex:一种简单的方法是切片和重新索引:
target = ['A', 'C']
out = df[target].reindex(df.columns, axis=1)
For in place modification you can take advantage of index difference:对于就地修改,您可以利用索引差异:
df[df.columns.difference(target)] = float('nan')
output:输出:
A B C D E F G H I
0 1 NaN 3 NaN NaN NaN NaN NaN NaN
1 1 NaN 3 NaN NaN NaN NaN NaN NaN
2 1 NaN 3 NaN NaN NaN NaN NaN NaN
*NB. *注意。 if you really want spaces as filling values:
如果你真的想要空格作为填充值:
# option 1:
out = df[target].reindex(df.columns, axis=1, fill_value='')
# option 2:
df[df.columns.difference(target)] = ''
# output:
A B C D E F G H I
0 1 3
1 1 3
2 1 3
target = ['A', 'C']
columns = [column for column in df.columns if column not in target]
df[columns] = float('nan')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.