Doing some data cleaning in a CSV file. I want to convert some CSV data into HTML before uploading the data to a website.
I'm going through every cell in the column called 'Details' in a pandas dataframe.
If a cell starts with this character combination: \r\r\n \t , then I want to replace it with this: <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
( ^
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
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.