小编Lev*_*tor的帖子

Python Pandas如何在不显式列出列的情况下从DataFrame中选择包含一个或多个空值的行?

我有一个〜300K行和~40列的数据帧.我想知道是否有任何行包含空值 - 并将这些'null'行放入一个单独的数据帧中,以便我可以轻松地探索它们.

我可以明确地创建一个掩码:

mask = False
for col in df.columns: 
    mask = mask | df[col].isnull()
dfnulls = df[mask]
Run Code Online (Sandbox Code Playgroud)

或者我可以这样做:

df.ix[df.index[(df.T == np.nan).sum() > 1]]
Run Code Online (Sandbox Code Playgroud)

是否有一种更优雅的方式(找到包含空值的行)?

python null nan pandas

198
推荐指数
4
解决办法
26万
查看次数

将大型DataFrame输出到CSV文件的最快方法是什么?

对于python/pandas,我发现df.to_csv(fname)的工作速度约为每分钟1百万行.我有时可以将性能提高7倍,如下所示:

def df2csv(df,fname,myformats=[],sep=','):
  """
    # function is faster than to_csv
    # 7 times faster for numbers if formats are specified, 
    # 2 times faster for strings.
    # Note - be careful. It doesn't add quotes and doesn't check
    # for quotes or separators inside elements
    # We've seen output time going down from 45 min to 6 min 
    # on a simple numeric 4-col dataframe with 45 million rows.
  """
  if len(df.columns) <= 0:
    return
  Nd = len(df.columns)
  Nd_1 = …
Run Code Online (Sandbox Code Playgroud)

python performance pandas output

20
推荐指数
4
解决办法
2万
查看次数

标签 统计

pandas ×2

python ×2

nan ×1

null ×1

output ×1

performance ×1