Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove string after a predefined string from a nested list of dictionaries with pandas

I have a dataframe with the following structure:

[
    {
        "key1":"value1",
        "key2":"2",
        "key3":["a","b2","keep this exemple from work_text_reviews_count of 450"],
    },
    {
        "key1":"value1",
        "key2":"2",
        "key3":[],
    }
]

How can i Remove string from predefined string with pandas without changing the structure.

the predefined string = "from work_text_reviews_count of"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the text that i want to remove "from work_text_reviews_count of 450"

The expected output:

[
    {
        "key1":"value1",
        "key2":"2",
        "key3":["a","b2","keep this exemple"],
    },
    {
        "key1":"value1",
        "key2":"2",
        "key3":[],
    }
]

>Solution :

You don’t have much choice here but to loop.

updated question:
pat = " from work_text_reviews_count of"
df['key3'] = [[x.split(pat)[0] for x in l] for l in df['key3']]

output:

     key1 key2                        key3
0  value1    2  [a, b2, keep this exemple]
1  value1    2                          []
older example

To update the data in place:

for l in df['details']:
    for d in l:
        if "average_rating" in d:
            d["average_rating"] = d["average_rating"].split()[0]

output:

    name                                                               details
0  Book1   [{'id': 30278752, 'isbn': ' 1594634025', 'average_rating': '3.92'}]
1  Book2  [{'id': 34006942, 'isbn': '  1501173219', 'average_rating': '4.33'}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading