## Python Pandas make calculation in single cell

I have a TYPE column and a VOLUME column What I'm looking to do if first check if TYPE column == 'var1'

If so I would like to make a calculation in the VOLUME column.

So far I have something like this:

data.loc[data['TYPE'] == 'var1', ['VOLUME']] * 2 data.loc[data['TYPE'] == 'var1', ['VOLUME']] * 4

This seems to set the entire column that meets the condition to the last variable. So I end up with just two values.

Out:

4 4 4 4 8 8 8

Another option:

data['VOLUME'] = data.loc[data['TYPE'] == 'var1', ['VOLUME']] * 2

This works for the first condition but show NaN for the second condition Then when I run:

data['VOLUME'] = data.loc[data['TYPE'] == 'var2', ['VOLUME']] * 4

The whole column show as NaN.

Consider a simple example which demonstrates what is happening.

df = pd.DataFrame({'A': [1, 2, 3]}) df A 0 1 1 2 2 3

Now, only values below 2 in column "A" are to be modified. So, try something like

df.loc[df.A < 2, 'A'] * 2 0 2 Name: A, dtype: int64

This series only has 1 row at index 0. If you try assigning this back, the implicit assumption is that the other index values are to be reset to NaN.

df.assign(A=df.loc[df.A < 2, 'A'] * 2) A 0 2.0 1 NaN 2 NaN

What we want to do is to modify *only* the rows we're interested in. This is best done with the in-place modification arithmetic operator `*=`

:

df.loc[df.A < 2, 'A'] *= 2

In your case, it is

data.loc[data['TYPE'] == 'var1', 'VOLUME'] *= 2

**Cookbook — pandas 1.1.0 documentation,** I have a TYPE column and a VOLUME column What I'm looking to do if first check if TYPE column == 'var1'. If so I would like to make a calculation in the� Create an Excel Sheet import pandas as pd writer = pd.ExcelWriter('demo.xlsx', engine='xlsxwriter') writer.save(). This code will create a new demo.xlsx file with a default sheet named Sheet1.

You are really close. The problem is in how you are storing the result. This should work:

data.loc[data['TYPE'] == 'var1', ['VOLUME']] = data['VOLUME'] * 2

**How to create new columns derived from existing columns?,** Minor tweaks might be necessary for earlier python versions. Efficiently and dynamically creating new columns using applymap In [69]: df Out[69]: One Two X Y X Y row 0 1.1 1.2 1.11 1.22 1 1.1 1.2 1.11 1.22 2 1.1 1.2 1.11 1.22 # Now stack & Reset Rolling Computation window based on values instead of counts. Python’s Pandas Library provides an member function in Dataframe class to apply a function along the axis of the Dataframe i.e. along each row or column i.e. DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)

You can use `*=`

with loc:

In [11]: df = pd.DataFrame([[1], [2]], columns=["A"]) In [12]: df Out[12]: A 0 1 1 2 In [13]: df.loc[df.A == 1, "A"] *= 3 In [14]: df Out[14]: A 0 3 1 2

**Common Excel Tasks Demonstrated in Pandas,** The calculation of the values is done element_wise. This means all values in the given column are multiplied by the value 1.882 at once. You do not need to use� Note also that row with index 1 is the second row. Row with index 2 is the third row and so on. If you’re wondering, the first row of the dataframe has an index of 0. That’s just how indexing works in Python and pandas. Extracting a single cell from a pandas dataframe ¶ df2.loc["California","2013"]

**How to change/update cell value in Python Pandas dataframe ,** For Excel, I have added the formula sum(G2:I2) in column J. Here is what it looks like in Excel: Excel sum It is very simple to add totals in cells in Excel for each month. Now that we have a nicely formatted DataFrame, we can add it to our existing one using append . Make sure to get it and install it first. In this Pandas with Python tutorial video with sample code, we cover some of the quick and basic operations that we can perform on our data. Say you have a data set that you want to add a moving average to, or maybe you want to do some mathematics calculations based on a few bits of data in other columns, adding the result to a new column.

I have the following dataframe Name Age 0 Mike 23 1 Eric 25 2 Donna Python; How to change/update cell value in Python Pandas. You can use the at() method to do this I have attached one example for your reference. Python Operator Pandas Method(s) + add()-sub(), subtract() * mul(), multiply() / truediv(), div(), divide() // floordiv() % mod() ** pow()

Varun September 15, 2018 Python: Add column to dataframe in Pandas ( based on other column or list or default value) 2020-06-28T17:03:19+05:30 Data Science, Pandas, Python 1 Comment In this article we will discuss different ways to how to add new column to dataframe in pandas i.e. using operator [] or assign() function or insert() function or

##### Comments

- Please add a sample of the input data, and a sample of what you want the output to look like, to make a minimal reproducible example
- @CamBam If your question was answered, please consider marking an answer accepted.
- You will calculate the multiplication for the entire column and then proceed to discard the entries you don't need. Good, but not as good as it could be (check out the other answers).