[Python] Scientific Notation and Significant Figures in Pandas DataFrame [duplicate]

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I have a few different NumPy arrays of data, and some of this data is in microfarads, microseconds, etc. for an upper-level physics class I am in. All of my calculations however have to be done in farads, seconds, etc., so I converted all of my units using a defined function I found elsewhere this site. Besides the point! Here's my dilemma:

    I am trying to create a dataframe with all of this data, and I am getting (ex) capacitance values that look like this: [3.50000000e-08, 3.545454545e-08, 3.38461254e-08, ... ] and I would like my data to look like this: [3.50e-08, 3.55e.08, 3.38e-08, ...]. How can I make this happen? np.round() of course does not work because if I write in np.round[capacitance, 3] then I just receive [0.0, 0.0, 0.0, ...].

    To make this question a bit more searchable (in case anyone else runs into this issue in the future): how can I format a numpy array to show me a certain number of significant figures in a pandas dataframe while also keeping all of these reported values in scientific notation? I want all of the nitty gritty decimal place values stored in the original arrays, but again I want all of the indices formatted in scientific notation in the pandas dataframe. Thank you in advance!!

    P.S.: For anyone that is curious and needs this themselves, here is code I found on here for SI unit conversion:

    def convert_si(val: float, unit_in: str, unit_out: str) -> float:
    SI = {'micro': 0.000001 , 'milli': 0.001, 'centi': 0.01, 'unit': 1.0, 'kilo': 1000.0}
    return val * SI[unit_in] / SI[unit_out]


    tau_v_rise = [0.00035 0.00039 0.00041 0.00044 0.00047 0.0005 0.00053 0.00054 0.00057
    0.0006 0.00063]

    tau_test = np.transpose(list(map('{:.2e}'.format,tau_v_rise)))
    print(tau_test)
    np.shape(tau_test)

    # This gives ['3.50e-04' '3.90e-04' '4.10e-04' '4.40e-04' '4.70e-04' '5.00e-04'
    '5.30e-04' '5.40e-04' '5.70e-04' '6.00e-04' '6.30e-04']
    # I need the quotation marks to go away though

    # np.shape(tau_test) gives (11,)
    # I don't really know what's going on with that

    # print(np.array(f"{tau_v_rise:.2e}"))
    # list(map(f"{tau_v_rise:.2e}"))

    Continue reading...

Compartilhe esta Página