[英]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.