简体   繁体   English

Go 通过单个列的单元格,如果它们满足使用 Pandas 的特定条件,则对其应用公式?

[英]Go through cells of a single column, and apply a formula to them if they meet a certain condition using Pandas?

Doing some data cleaning in a CSV file.在 CSV 文件中进行一些数据清理。 I want to convert some CSV data into HTML before uploading the data to a website.在将数据上传到网站之前,我想将一些 CSV 数据转换为 HTML。

I'm going through every cell in the column called 'Details' in a pandas dataframe.我将浏览 pandas dataframe 中名为“详细信息”的列中的每个单元格。

If a cell starts with this character combination: \r\r\n \t , then I want to replace it with this: <ul><li>如果一个单元格以这个字符组合开头: \r\r\n \t ,那么我想用这个替换它: <ul><li>

 df2 = df.copy() def startswith_replace (x, a, b): if x.startswith(a): x.replace(a, b) df2['Details'] = df2['Details']. apply(lambda x: startswith_replace(x, '\\r\\r\\n \\t', '\<ul\>\<li\>'))

When I run this, however, every cell in the 'Details' column is replaced with 'None' as its value.但是,当我运行它时,“详细信息”列中的每个单元格都被替换为“无”作为其值。

This can be accomplished using the built-in Series.str.replace without needing to define your own function, with just a little regex这可以使用内置的Series.str.replace来完成,而无需定义自己的 function,只需一点正则表达式

( ^ to only check the start of the string and () optionally to set it as a capture group, but if you decide you want to replace all occurrences both can be omitted and the raw string passed) ^仅检查字符串的开头, ()可选择将其设置为捕获组,但如果您决定要替换所有出现的位置,则两者都可以省略并传递原始字符串)

df

    A   B   A   Details
0   1   2   3   \r\r\n \t
1   4   5   6   lkjn \r\r\n \t
2   7   8   9   abcdefg

df['Details']=df['Details'].str.replace(r'^(\r\r\n \t)','\<ul\>\<li\>')

    A   B   A   Details
0   1   2   3   \<ul\>\<li\>
1   4   5   6   lkjn \r\r\n \t
2   7   8   9   abcdefg

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

相关问题 如果某些条件满足熊猫,如何翻转列 - How to flip column if certain condition is meet pandas 根据特定列中的条件应用公式 - Apply formula based on condition in certain column 使用 Pandas,如何根据给定条件下前几行的平均值将公式应用于多行? - Using Pandas, how to I apply a formula to several rows, based on the average of previous rows given a certain condition? 删除熊猫中不符合条件的单元格 - Remove cells that do not meet condition in pandas Pandas .apply():如何在 apply() 中使用涉及同一列中前面单元格值的公式? - Pandas .apply(): How to use a formula in apply() that involves values from preceding cells in the same column? 当且仅当其他列满足特定条件时,Pandas 数据框会计算特定值在列中出现的次数 - Pandas Data frame counting how many times certain value appears in column if and only if other columns meet certain condition 无法使用 pandas 将公式应用于 dataframe 第一列值 - unable to apply formula to dataframe first column value using pandas 使用 pandas 适用于 if 条件 - Using pandas apply with if condition 在Pandas数据框中满足特定条件的所有行的均值 - Mean of all rows which meet a certain condition in Pandas dataframe 计算满足熊猫数据框中某些求和条件的行数 - Counting the number of rows that meet certain sum condition in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM