calculusqu33n
calculusqu33n

Reputation: 1

Scientific Notation and Significant Figures in Pandas DataFrame

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}"))

Upvotes: 0

Views: 68

Answers (1)

rehaqds
rehaqds

Reputation: 2000

To format numbers display in Numpy with scientific notation and 3 digits precision, use np.set_printoptions:

tau = np.array([3.50000000e-08, 3.545454545e-08, 3.38461254e-08])
np.set_printoptions(precision=3) 
print(tau)
[3.500e-08 3.545e-08 3.385e-08]

To format numbers display in Pandas, you can use pd.set_option:

df = pd.DataFrame(tau, columns=['tau'])
pd.set_option('display.float_format', '{:,.3E}'.format)

but this will change the format for all your future prints (except if you use a new pd.set_option). If you just want to change the format for one display, better use the context:

with pd.option_context('display.float_format', '{:,.3E}'.format):
    print(df)

    tau
0   3.500E-08
1   3.545E-08
2   3.385E-08

Upvotes: 0

Related Questions