Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14011

Pandas dataframe: how to multiply a specific value by using its row index?

$
0
0

I want to change specific values in a Pandas dataframe. Here is an example dataframe (in reality, there are many more rows):

              Value                       Property0               CH4                       Type1          -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] * 10df["Value"].replace(df.iloc[row_index][0], new_value, inplace=True)

However, this gives me weird output. I get:

              Value                       Property0               CH4                       Type1 -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?


Viewing all articles
Browse latest Browse all 14011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>