Home > Software engineering >  Remove string after a predefined string from a nested list of dictionaries with pandas
Remove string after a predefined string from a nested list of dictionaries with pandas

Time:03-21

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"

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":[],
    }
]

CodePudding user response:

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'}]
  • Related