Issue
I have a problem with pandas dataframe. I want to modify my dataframe. Columns name is "cascade". How can I edit it. If you solve problems like this please help me to solve this issue or any ideas for this.
To this:
Solution
Assuming the cascade
column looks like this:
["['(E12)', '(E14)']", "['(E81)', '(E82)']"]
To convert it into:
[['(E12)', '(E14)'], ['(E81)', '(E82)']]
You can use:
df['cascade'] = df['cascade'].str.strip('[]').str.replace("'", "").str.split(', ')
Explanation:
- First the .str accessor is used to transform the
pandas.core.series.Series
object to apandas.core.strings.accessor.StringMethods
object in order to apply vectorized string operations to thecascade
column of the dataframe - Then .strip() is called to strip the brackets from each element of the series,
- The previous
.strip()
returned a newpandas.core.series.Series
object which we need to convert into a newpandas.core.strings.accessor.StringMethods
object in order to apply the next.replace()
call. - Similarly, for the final
.str.split(', ')
Answered By - Håkon Hægland
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.