3 回答
TA贡献1831条经验 获得超9个赞
根据问题陈述,您希望降低 nan 值的优先级,并将非 nan 值置于顶部。
import numpy as np
import pandas as pd
import functools
def drop_and_roll(col, na_position='last', fillvalue=np.nan):
result = np.full(len(col), fillvalue, dtype=col.dtype)
mask = col.notnull()
N = mask.sum()
if na_position == 'last':
result[:N] = col.loc[mask]
elif na_position == 'first':
result[-N:] = col.loc[mask]
else:
raise ValueError('na_position {!r} unrecognized'.format(na_position))
return result
df = pd.read_table('data', sep='\s{2,}')
print(df.apply(functools.partial(drop_and_roll, fillvalue='')))
TA贡献1884条经验 获得超4个赞
假设您要回填值,然后删除任何列中显示的任何重复项,此示例有效:
import pandas as pd
import numpy as np
data = [
['POINT_1.1', 'POINT_1.2', pd.NA],
[pd.NA, pd.NA, 'POINT_1.3'],
['POINT_2.1', 'POINT_2.2', pd.NA],
[pd.NA, pd.NA, 'POINT_2.3']
]
df = pd.DataFrame(data)
df
# 0 1 2
# 0 POINT_1.1 POINT_1.2 <NA>
# 1 <NA> <NA> POINT_1.3
# 2 POINT_2.1 POINT_2.2 <NA>
# 3 <NA> <NA> POINT_2.3
t = df.T.bfill().T.bfill()
t
# 0 1 2
# 0 POINT_1.1 POINT_1.2 POINT_1.3
# 2 POINT_2.1 POINT_2.2 POINT_2.3
for column in t.columns:
t = t.drop_duplicates(column)
t
# 0 1 2
# 0 POINT_1.1 POINT_1.2 POINT_1.3
# 2 POINT_2.1 POINT_2.2 POINT_2.3
添加回答
举报