在pandas DataFrame中将非空单元格移到左侧 [英] Move non-empty cells to the left in pandas DataFrame
本文介绍了在pandas DataFrame中将非空单元格移到左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下形式的数据
Suppose I have data of the form
Name h1 h2 h3 h4
A 1 nan 2 3
B nan nan 1 3
C 1 3 2 nan
我想将所有非nan单元格移到左侧(或在新列中收集所有非nan数据),同时保留从左到右的顺序,得到
I want to move all non-nan cells to the left (or collect all non-nan data in new columns) while preserving the order from left to right, getting
Name h1 h2 h3 h4
A 1 2 3 nan
B 1 3 nan nan
C 1 3 2 nan
我当然可以逐行这样做.但我希望知道是否还有其他方法可以改善性能.
I can of course do so row by row. But I hope to know if there are other ways with better performance.
推荐答案
这就是我所做的:
我将您的数据框拆开成更长的格式,然后按名称列分组.在每个组中,我放下NaN,然后将其重新索引为以h4为底的完整h1,从而在右侧重新创建您的NaN.
I unstacked your dataframe into a longer format, then grouped by the name column. Within each group, I drop the NaNs, but then reindex to the full h1 thought h4 set, thus re-creating your NaNs to the right.
from io import StringIO
import pandas
def defragment(x):
values = x.dropna().values
return pandas.Series(values, index=df.columns[:len(values)])
datastring = StringIO("""\
Name h1 h2 h3 h4
A 1 nan 2 3
B nan nan 1 3
C 1 3 2 nan""")
df = pandas.read_table(datastring, sep='\s+').set_index('Name')
long_index = pandas.MultiIndex.from_product([df.index, df.columns])
print(
df.stack()
.groupby(level='Name')
.apply(defragment)
.reindex(long_index)
.unstack()
)
所以我得到了
h1 h2 h3 h4
A 1 2 3 NaN
B 1 3 NaN NaN
C 1 3 2 NaN
这篇关于在pandas DataFrame中将非空单元格移到左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文