Issue
I want to change specific values in a Pandas dataframe. Here is an example dataframe (in reality, there are many more rows):
Value Property
0 CH4 Type
1 -10.90979 Density (g/cm3)
2 5.00000 Temperature (K)
Here I want to multiply "10.90979
" by 10
in the row labeled "1
". I don't want to have to write "10.90979 * 10
" because I only know that I have a property called "Density (g/cm3)
". I don't know its value. So I want to use the index of the row in which "Density (g/cm3)
" appears in the multiplication.
I have tried:
row_index = df.index.get_loc(df[df['Property'] == 'Density (g/cm3)'].index[0])
new_value = df.iloc[row_index][0] * 10
df["Value"].replace(df.iloc[row_index][0], new_value, inplace=True)
However, this gives me weird output. I get:
Value Property
0 CH4 Type
1 -10.90979-10.90979... Density (g/cm3)
2 5.00000 Temperature (K)
I can't post the details of the code but am hoping someone will recognize a simple mistake. I am not sure that I'm using multiplication correctly for a dataframe. I also tried using
df.iloc[row_index][0].mul(10)
but get the error AttributeError: 'str' object has no attribute 'mul'
.
Can anyone please point me in the right direction?
Solution
As others pointed out, your Value
column is not numerical (because of the 'CH4'
first value). But, more fundamentally, why do you want to multiply the density by 10
? What's the higher level problem?
From this question (and your previous one, now deleted), it looks like you have chemical compounds and are trying to do unit conversions on their physical properties. I would recommend looking into packages that do that readily. For example: pint
and its pandas companion pint_pandas
.
It seems that you have DataFrames with a chemical compound (e.g., 'SO2'
or 'CH4'
) as the value of the first row, and then some physical properties.
You may be better off using dicts to represent your properties. E.g.:
from pint import UnitRegistry
Q_ = UnitRegistry().Quantity
compounds = {
'CH4': dict(
name='methane',
molar_mass=Q_(16.043, 'g / mol'),
density_0=Q_(0.657, 'kg/m**3'),
at_0=Q_(25, '°C'),
density_1=Q_(0.717, 'kg/m**3'),
at_1=Q_(0, '°C'),
density_2=Q_(422.8, 'g/L'),
at_2=Q_(-162, '°C'),
),
'SO2': dict(
name='sulfur dioxide',
molar_mass=Q_(64.066, 'g / mol'),
density_0=Q_(2.6288, 'kg / m**3'),
at_0=Q_(25, '°C'),
),
}
Then, if you like, you can convert this into a DataFrame:
import pandas as pd
import pint_pandas
df = pd.DataFrame(compounds).T
and convert a specific column into some desired unit, for example:
>>> df['density_0'].astype('pint[g/L]')
CH4 0.6570000000000001
SO2 2.6288000000000005
Name: density_0, dtype: pint[gram / liter]
# or (shudder):
>>> df['density_0'].astype('pint[lb/gal]')
CH4 0.0054829307249767
SO2 0.02193839922346841
Name: density_0, dtype: pint[pound / gallon]
or convert the whole DataFrame to have consistent units for each column:
z = df.astype(dict(
molar_mass='pint[g/mol]',
density_0='pint[g/L]',
density_1='pint[g/L]',
density_2='pint[g/L]',
at_0='pint[degC]',
at_1='pint[degC]',
at_2='pint[degC]',
))
>>> z
name molar_mass density_0 at_0 density_1 at_1 density_2 at_2
CH4 methane 16.043 0.6570000000000001 25 0.7170000000000001 0 422.8 -162
SO2 sulfur dioxide 64.066 2.6288000000000005 25 nan nan nan nan
>>> z.dtypes
name object
molar_mass pint[gram / mole]
density_0 pint[gram / liter]
at_0 pint[degree_Celsius]
density_1 pint[gram / liter]
at_1 pint[degree_Celsius]
density_2 pint[gram / liter]
at_2 pint[degree_Celsius]
dtype: object
Answered By - Pierre D
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.