Home > database >  (Python) How to only keep specific part of cells in a dataframe
(Python) How to only keep specific part of cells in a dataframe

Time:12-11

I want to cleanup a source column of my dataframe. At the end I only want to keep the part behind 'name'.

What is the best way to do this? For example:

row 1, column 1: {'id': 'rtl-nieuws', 'name': 'RTL Nieuws'}

row 2, column 1: {'id': 'none', 'name': 'www.ad.nl'}

Desired outcome:

row 1, column 1: RTL Nieuws

row 2, column 1: www.ad.nl

CodePudding user response:

Is this what you are trying to do? In the future, please consider giving a working example to solve the request from.

data = pd.DataFrame({
    "id": ["rtl-nieuws", "none"],
    "name": ["RTL Nieuws", "www.ad.nl"]
}, index=[0,1])


data.drop("id", axis = 1)

#     name
# 0   RTL Nieuws
# 1   www.ad.nl

CodePudding user response:

Considering your data seems to be in the format of a dictionary, you can use ast.literal_eval() to access the value at the 'name' key.

import ast

current_cell = "{'id': 'rtl-nieuws', 'name': 'RTL Nieuws'}"
name = ast.literal_eval(current_cell)['name']
print(name)

>>> RTL Nieuws

  • Related