【问题标题】:Only remove entirely empty rows in pandas [duplicate]只删除熊猫中完全空的行[重复]
【发布时间】:2021-03-07 23:08:48
【问题描述】:

如果我有这个数据框:

d = {'col1': [1, np.nan, np.nan], 'col2': [1, np.nan, 1]}
df = pd.DataFrame(data=d)

    col1    col2
0   1.0     1.0
1   NaN     NaN
2   NaN     1.0

并且只想删除空行以生成以下内容:

d = {'col1': [1, np.nan], 'col2': [1, 1]}
df = pd.DataFrame(data=d)

    col1    col2
0   1.0     1
1   NaN     1

最好的方法是什么?

【问题讨论】:

  • 请注意,df.dropna(how='all') 不会将 col2 转换为 int。您可能必须明确地这样做。

标签: python pandas


【解决方案1】:

查看docs页面

df.dropna(how='all')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    • 2021-09-05
    • 1970-01-01
    • 2020-11-29
    • 2020-11-09
    相关资源
    最近更新 更多