如果值不为空,Python 处理特定 Pandas DataFrame 列的最佳方法

标签 python pandas dataframe null nan

仅当一组特定 Dataframe 列中的值不为空时,处理该值的最佳方法是什么?

我的原始代码:

for i in columns_to_process:
    df[i] = df[i].astype(str) + '!'

#columns_to_process is a list of selected df column name strings

我意识到这会将 null 值变成 nan! 但我只想将它们保留为 null。

我已经研究过使用 .apply()lambda 函数,但根据:Pandas: How can I use the apply() function for a single column?,这在处理单个列时显然是不好的做法。 。似乎 .apply() 更多的是用于更新数据框中的每一列。

然后我发现了这个:replace non Null values in column by 1并设法制定了这个可行的解决方案:

for i in columns_to_process:
    df.loc[df[i].notnull(), i] = df.loc[df[i].notnull(), i].astype(str) + '!'

我花了很长时间才弄清楚这一点,而且看起来不太优雅,所以我想知道是否有更好/更Pythonic的方法来做到这一点?

最佳答案

IIUC,尝试pandas.DataFrame.where:

# Sample df
df = pd.DataFrame({'foo': ['one', 'one', 'one', 'two', 'two',
                            'two'],
                   'bar': ['A', 'B', np.nan, 'A', 'B', 'C'],
                   'baz': [1, 2, np.nan, 4, 5, 6],
                   'zoo': ['x', 'y', 'z', 'q', 'w', 't']})

columns_to_process = ['bar', 'baz']
df[columns_to_process] = df[columns_to_process].where(df[columns_to_process].isna(), lambda x: x.astype(str)+'!')
df

输出:

   bar    baz  foo zoo
0  A!!  1.0!!  one   x
1  B!!  2.0!!  one   y
2  NaN    NaN  one   z
3  A!!  4.0!!  two   q
4  B!!  5.0!!  two   w
5  C!!  6.0!!  two   t

关于如果值不为空,Python 处理特定 Pandas DataFrame 列的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59659983/

相关文章:

python - 字符串文字和字符串值之间有什么区别?

python - Pandas 在多索引上应用函数

python - 尝试根据与数据框相关的 if 语句在 Pandas 中创建新的数据框列

python - 在 pandas 中随机更改行

python - 合并具有不同索引的 3 个数据帧

r - 在前几行中创建包含源/版权信息的 .csv 或 .txt 文件,以便从 Shiny 的导出

python - 如何用 Python 解决这个数学难题?

python - 从列表中查找最小整数(缺失数字)

python - 类型错误 : '...' has type str, 但需要以下之一:字节

python - 过滤在单元格中具有数组的 Pandas 数据框