简体   繁体   中英

Python Pandas copy string from one cell to another based on multiple conditions (length and content)

How to copy values in a cell from one column to another if they meet a specific condition. In this case, the length of the content of the cell (Greater than 1 ) and the contents of the cell (Does not include the letter 'a').

import numpy as np
import pandas as pd

data= np.array([[ 'a','b','c','xx','yy'], [ 'zz','ff','aa','a','b']])
dataset = pd.DataFrame(data.T, columns = ['col1', 'col2'])

   col1 col2
0   a   zz
1   b   ff
2   c   aa
3   xx  a
4   yy  b

The output would look like:

   col1 col2
0   a   zz
1   b   b
2   c   c
3   xx  a
4   yy  b

Here is where I stopped:

for x in dataset['col1']:
     if len(x) == 1 :
         ##replace col2 value with that of x

pandas.Series.where

mask1 = dataset.col2.str.len() > 1
mask2 = dataset.col1.ne('a')

dataset.assign(col2=dataset.col1.where(mask1 & mask2, dataset.col2))

  col1 col2
0    a   zz
1    b    b
2    c    c
3   xx    a
4   yy    b

在这种情况下,您可以使用.loc的第二个参数,它允许您设置一个特定的列名称,该名称将“接收” =运算符右侧设置的值:

dataset.loc[dataset['col2'].str.len() > 1, 'col2'] = dataset['col1']

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