简体   繁体   English

如何遍历pandas数据帧,检查条件,执行字符串操作并写入新列?

[英]How to loop through pandas dataframe, check conditions, perform string manipulations & write to a new column?

I have a dataframe like below; 我有一个如下数据框;

--------------------------------
Col1    Col2                    
--------------------------------
1       AppVer: 1.1.1 | name: A 
0       name:B                  
1       AppVer: 2.3.1 | name: B 

I wanted to create a new column (newCol3) based on the condition 1. If Col1=1 then split the Col2 based on "|" 我想根据条件1创建一个新列(newCol3)。如果Col1 = 1,则根据“|”拆分Col2 and write to the column newCol3 2. If Col1=0 then write "Not Applicable" to the column newCol3 并写入newCol3列2.如果Col1 = 0,则将“Not Applicable”写入newCol3列

I tried the below code for loop using iterrows & conditional statements; 我使用iterrows和条件语句尝试了下面的代码循环;

for index, row in df1.iterrows():
    if row['Col1']==1:
        df1['newCol3']="NA"
    elif row['Col1']==0:
        a=row['Col2'].split("|")
        df1['newCol3']=a[0]

But i the value in newCol3 is not as expected as shown below. 但我在newCol3中的值不如预期,如下所示。 Also, i get a warning like this " main :8: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy " 另外,我得到一个像这样的警告main :8:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置一个值。尝试使用.loc [row_indexer,col_indexer] = value请参阅文档中的警告: http//pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Obtained Output: 获得的输出:

---------------------------------------------------
Col1    Col2                        newCol3
---------------------------------------------------
1       AppVer: 1.1.1 | name: A     1.1.1
0       name:B                      1.1.1
1       AppVer: 2.3.1 | name: B     2.3.1

Expected Output: 预期产出:

---------------------------------------------------
Col1    Col2                        newCol3
---------------------------------------------------
1       AppVer: 1.1.1 | name: A     1.1.1
0       name:B                      Not Applicable
1       AppVer: 2.3.1 | name: B     2.3.1

Provide me any help/suggestions. 向我提供任何帮助/建议。

In your case I would suggest using loc to create a new column. 在你的情况下,我建议使用loc来创建一个新列。

Docs: loc 文件: loc

Docs: str expand Docs: str扩展

Docs for str extract: str.extract str提取的文档: str.extract

df.loc[df['Col1']==1,'Col3'] = df['Col2'].str.extract(pat='insert the pattern here')
df.loc[df['Col1']==0,'Col3'] = 'Not Applicable'

Just saw the expected output. 刚看到预期的输出。 Read the docs I linked and change the str.extract as required. 阅读我链接的文档并根据需要更改str.extract

I feel like you can do 我觉得你能做到

df['New']=df.Col2.str.extract('(\d*\.?\d+\.?\d+)').fillna('Not Applicable')
df
Out[43]: 
   Col1                      Col2             New
0     1  AppVer: 1.1.1 | name: A            1.1.1
1     0  name:B                    Not Applicable
2     1  AppVer: 2.3.1 | name: B            2.3.1

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

相关问题 检查元素是否在列表中,然后在满足条件时写入 Pandas dataframe 中的新列 - Check if element is in list, then write to new column in Pandas dataframe if conditions met 如何对 Pandas DataFrame 上的类别执行操作 - how to perform manipulations on categories on pandas DataFrame 如何使用 Python Pandas 中的条件循环遍历 dataframe 列名以获得最小值? - How to loop through a dataframe column name with conditions in Python Pandas to get min value? 遍历 pandas dataframe 中的行并更新对列条件的检查 - Loop through rows in pandas dataframe and update check on the column condition 如何在python pandas数据帧中使用日期执行&gt;=或&lt;=条件 - How to perform >= or <= conditions with date in python pandas dataframe 如何在两个条件下在pandas数据框中添加新列? - How to add new column in pandas dataframe with two conditions? 如何根据此 Pandas 数据框中的列条件创建新的索引行? - How to create new index lines based on column conditions in this pandas dataframe? 嵌套if条件以在pandas数据框中创建新列 - Nested if conditions to create a new column in pandas dataframe 基于布尔条件的 Pandas 数据框中的新列 - New column in Pandas dataframe based on boolean conditions 在 dataframe 中,如何根据另一列的条件在另一列中写入字符串 - In a dataframe, how to write a string in one column based on conditions in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM