简体   繁体   English

我想基于上一列添加一个新的 DataFrame 列,以便如果上一列元素与列表值匹配,则更改该值

[英]I want to add a new DataFrame column based on previous column such that if previous column element matches with list value, change the value

Input df输入 df

Index       col1
0     Img    
1     Fruit  
2     Img    
3     Ball    
4     Ball    
5     Fruit    
6     shirt    
7     Fruit 

Map list to input df将列表映射到输入 df

list1 = ['Img_A_10', 'Fruit_A_100', 'Ball_B_120']

Output df输出 df

     col1      col22
0     Img    Img_A_10
1     Fruit  Fruit_A_100
2     Img    Img_A_10
3     Ball   Ball_B_120
4     Ball   Ball_B_120
5     Fruit  Fruit_A_100  
6     shirt  shirt         
7     Fruit  Fruit_A_100

try this,尝试这个,

df['col2'] = df.col1.map({k.split("_")[0]: k for k in list1}).fillna(df.col1)

or或者

df['col2'] = df.col1.replace({k.split("_")[0]: k for k in list1})

df
Out[93]: 
    col1         col2
0    Img     Img_A_10
1  Fruit  Fruit_A_100
2    Img     Img_A_10
3   Ball   Ball_B_120
4   Ball   Ball_B_120
5  Fruit  Fruit_A_100
6  shirt        shirt
7  Fruit  Fruit_A_100

just in case splits doesn't match (example : A_Fruit_100 ) , you can extract then replace以防万一拆分不匹配(例如: A_Fruit_100 ),您可以extract然后replace

s = pd.Series(list1)
d = dict(zip(s.str.extract('('+'|'.join(df['col1'])+')',expand=False),s))
df['col22'] = df['col1'].replace(d)

print(df)
        col1        col22
Index                    
0        Img     Img_A_10
1      Fruit  Fruit_A_100
2        Img     Img_A_10
3       Ball   Ball_B_120
4       Ball   Ball_B_120
5      Fruit  Fruit_A_100
6      shirt        shirt
7      Fruit  Fruit_A_100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas Dataframe基于前一行,将值添加到新列,但该列的最大值限于该列 - Pandas Dataframe Add a value to a new Column based on the previous row limited to the maximum value in that column Spark使用上一行的值将新列添加到数据框 - Spark add new column to dataframe with value from previous row 从 dataframe 获取上一个和下一个值并添加一个新列 - getting the previous and next value from a dataframe and add a new column 如何在 dataframe 的中间添加一个新列,其值基于上一列? - How to add a new column in the middle of the dataframe with values based on the previous column? 如果上一列中的对应项在列表中,则将新列添加到 pandas dataframe - Add a new column to a pandas dataframe if the corresponding item in the previous column is in a list Dask 基于上一列添加新列 - Dask Add New Column Based on the Previous Column 具有先前行值的新列 - New column with previous rows value Pandas DataFrame:添加具有基于前一行计算值的新列 - Pandas DataFrame: Add new column with calculated values based on previous row 基于我想分离列并在不同数据框中添加值的条件 - based on a condition i want to separate the column and add the value in different dataframe Python Pandas Dataframe 根据同一列中的前一行值计算新行值 - Python Pandas Dataframe calculating new row value based on previous row value within same column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM