简体   繁体   中英

Applying an operation to a subset of the columns in a Dataframe in pandas

Given a DataFrame such as:

   0  1  2
0 'a' 6  7  
1 'a' 8  9   

Where the first column consists of strings and the other columns are integers. I want to apply multiplication by a constant to columns 1 and 2 only. That is, avoiding multiplying the strings as well. This is not acheived by df = df*cnst . What is the cleanest way to do this?

Here is a bit more generic solution:

Data:

In [200]: df
Out[200]:
     A  B    C          D
0  aaa  6  7.1 2001-01-01
1  bbb  8  9.2 2017-02-13

Let's find all numeric columns:

In [201]: num_cols = df.columns[df.dtypes.map(lambda x: np.issubdtype(x, np.number))]

In [202]: num_cols
Out[202]: Index(['B', 'C'], dtype='object')

Now we can do math on numeric columns only:

In [203]: df[num_cols] *= 10

Result:

In [204]: df
Out[204]:
     A   B     C          D
0  aaa  60  71.0 2001-01-01
1  bbb  80  92.0 2017-02-13

Dtypes:

In [205]: df.dtypes
Out[205]:
A            object
B             int64
C           float64
D    datetime64[ns]
dtype: object

You can just use iloc (for integer position-based indexing) or loc (for label-based indexing) to get the subset of your columns you wish to multiply. In this case either would work, as your columns appear to be labelled numerically. We can also use the augmented assignment operator *= .

df.iloc[:, [1, 2]] *= cnst

Further basic info can be found in the Indexing and Selecting Data section of the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM