[Python] csv file wide to long. Float value gets truncated and getting integer for index column

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have csv file with approx 40K rows and 1200 columns in wide format. with looks something like this:

    | Millisec_diff | Value1 | Value2 | Value3 |
    | ------------- | ------ | ------ |------- |
    | 0 | 100 | 200 | 1.3 |
    | 0.005 | 101 | 20.1 | 1.3 |
    | 0.01 | 103 | 20.1 | 2.5 |
    | 0.015 | 104 | 24.1 | 4.5 |
    | 0.02 | 103 | 40.1 | 5.4 |


    I added date time column based Millisec_diff and converted to long format using pandas.melt:

    start = datetime.today()
    df['time'] = (
    datetime(
    start.year,
    start.month,
    start.day,
    start.hour,
    start.minute,
    start.second,
    start.microsecond)
    + pandas.to_timedelta(df['Millisec_diff '], unit='ms')
    )

    df_long = pandas.melt(
    df,
    id_vars=["time", 'Millisec_diff '],
    value_vars=col_names,
    var_name='param',
    value_name='value')


    When I run the code on sample dataset, I can see proper values (eg: .005, .01) for Millisec_diff but when I run this on whole file, I can see only integers (like 1, 2) for Millisec_diff instead of actual decimal values. I am not sure what is happening.

    Continue reading...

Compartilhe esta Página