[Python] in python why split not resulting at all

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    python3 why my code to split column 4 at the bases of "," to the column 7, 8 ,9 with condition if the column 5 vale is "LUB" not working

    source data is : source data image

    raw source data
    20240801 CASH MAN1 120.00 Z20/1 0.5 , Z20/1 ; 120 1 LUB
    20240801 PAYTM 15720.00 CASH MAN1 0 1 JRNLCR
    20240801 PAYTM 81343.00 CASH MAN2 0 2 JRNLCR


    my code is

    import numpy as np
    import pandas as pd

    var001_sf = input("Enter file name to import : ")
    var002_sf = var001_sf + ".csv"
    var002_ar1 = pd.read_csv(var002_sf, sep='\t', header=None)
    var002_ar1 = var002_ar1.dropna(axis=0) # remove rows with na value
    var002_ar1.iloc[:, [0, 1, 2, 3, 4, 5, 6]] = var002_ar1.iloc[:, [0, 1, 2, 3, 4, 5, 6]].map(str)
    var002_ar1.loc[var002_ar1.iloc[:, 6] == 'LUB', [7, 8, 9]] = var002_ar1.iloc[:, 4].str.split(', ', n=2, expand=True)
    var002_ar1.to_csv("t250221.csv", sep='\t')


    output data image

    raw output data

    20240801 CASH MAN1 120.0 Z20/1 0.5 , Z20/1 ; 120 1 LUB
    1 20240801 PAYTM 15720.0 CASH MAN1 0 1 JRNLCR
    2 20240801 PAYTM 81343.0 CASH MAN2 0 2 JRNLCR


    cant get split "0.5 , Z20/1 ; 120" in column 7, 8, 9

    what should be done for desired result

    Continue reading...

Compartilhe esta Página