Display Scientific Notation for certain Columns only, Pandas Dataframe

Viewed 229

I am trying to display results from a pandas dataframe, that contains small numbers.

dict1 = {
    'Name':['one', 'two', 'three', 'four'],
    'R1 (V)':[1.1,2.2,3.3,4.4],
    'R2 (mV)':[1.1e-3,2.2e-3,3.3e-3,4.4e-3],
    'R3 (uV)':[1.1e-6,2.2e-6,3.3e-6,4.4e-6],
    'R4 (nV)':[1.1e-9,2.2e-9,3.3e-3,4.4e-9],
    'R5 (pV)':[1.1e-12,2.2e-12,3.3e-12,4.4e-12]
}

df = pd.DataFrame.from_dict(dict1)

Output Data Frame

In my example here I want to change R3 to scientific notation (1.1e-6, 2.2e-6, ...) without changing how the other columns are displayed

2 Answers

try to edit the format of your DataFrame like this:

df.style.format({'R3 (uV)': "{:.2E}"})

this will output the DataFrame with R3 in the scientific notation, but if you try to display the DataFrame again you will have to repeat this procedure because the format is not permanent

You can convert the values to strings and apply the wanted notation.

df['R3 (uV)'] = df['R3 (uV)'].apply(lambda val: '%.2E' % val)
Related