如何仅删除 pandas 数据框中的空单元格并将值向上移动?

问题描述 投票:0回答:1

我可以使用 drop() 删除整行/列,如何删除单个单元格并向上移动下一个值而不删除整行。有没有内置的功能?

我的桌子是这样的

SMA_5 熊猫_5
54
34
54
32
51 89
94 90
51 70
32 31

我想要这样的结果:

SMA_5 熊猫_5
54 89
34 90
54 70
32 31
51 98
94 13
51 23
32 13
python pandas numpy jupyter-notebook
1个回答
0
投票

您可以一次只移动一列并将其向上移动

import pandas as pd
import numpy as np

def compress_column(df, column_name):
    result = df.copy()
    
    valid_values = result[column_name].dropna().values
    
    new_column = np.full(len(result), np.nan)
    
    new_column[:len(valid_values)] = valid_values
    
    result[column_name] = new_column
    
    return result

def compress_all_columns(df):
    result = df.copy()
    for column in df.columns:
        result = compress_column(result, column)
    return result

if __name__ == "__main__":
    test_data = {
        'Column1': [1, np.nan, 3, np.nan, 5],
        'Column2': [np.nan, 2, np.nan, 4, 5],
        'Column3': [1, 2, np.nan, np.nan, 5],
        'Column4': [np.nan, np.nan, 3, 4, 5]
    }
    
    df = pd.DataFrame(test_data)
    print(df)
    
    compressed_all = compress_all_columns(df)
    print(compressed_all)
    

它产生



   Column1  Column2  Column3  Column4
0      1.0      NaN      1.0      NaN
1      NaN      2.0      2.0      NaN
2      3.0      NaN      NaN      3.0
3      NaN      4.0      NaN      4.0
4      5.0      5.0      5.0      5.0

# after compression:

   Column1  Column2  Column3  Column4
0      1.0      2.0      1.0      3.0
1      3.0      4.0      2.0      4.0
2      5.0      5.0      5.0      5.0
3      NaN      NaN      NaN      NaN
4      NaN      NaN      NaN      NaN
© www.soinside.com 2019 - 2024. All rights reserved.