3 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
根據(jù)問(wèn)題陳述,您希望降低 nan 值的優(yōu)先級(jí),并將非 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貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
假設(shè)您要回填值,然后刪除任何列中顯示的任何重復(fù)項(xiàng),此示例有效:
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
添加回答
舉報(bào)