[Python] Processing data in python [closed]

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    Sample csv file records mentioned below and the actual records may contains huge data. I am beginner and have written the below snippet.

    Input file: sample.csv

    XXXXX
    A|B|C|D
    E||G|H
    J|K||M
    20


    Snippet: Based on below code to print df will contain the rows after removing header and trailer record new_df will contain the output formatted based on the delimiter

    import csv
    import pandas as pd
    file_path = 'test.csv'
    df = pd.read_csv('test.csv', header=None, skiprows=1, skipfooter=1, engine='python')
    new_df = pd.DataFrame(df[0].str.split('|'))
    print ("After removing header and trailer")
    print(df)
    print ("New dataframe")
    print (new_df)


    Output:

    After removing header and trailer
    0
    0 A|B|C|D
    1 E||G|H
    2 J|K||M

    New dataframe
    0
    0 [A, B, C, D]
    1 [E, , G, H]
    2 [J, K, , M]


    Question: How to add column check (2 and 3) in the new data frame(new_df) snippet to display the below message in the output.

    • Empty Column check: 2,3
    • Number of rows: 3 (After removing header and trailer)
    • Row 2: Col 2 is empty [E, , G, H]
    • Row 3: Col 3 is empty [J, K, , M]

    Continue reading...

Compartilhe esta Página