我有一个如下所示的数据框:
user_id event_desc
100013 {'firebase_screen': '/product-popup', 'banner_id': '9260'}
100022 {'firebase_screen': '/product-popup', 'banner_id': '9152'}
100076 {'firebase_screen_id': '-1531185473150377061'}
100076 {'banner_id': '8567', 'firebase_screen': '/product-popup'}
我正在尝试获取键banner_id的值。
因此,输出如下:
user_id event_desc banner_id
100013 {'firebase_screen': '/product-popup', 'banner_id': '9260'} 9260
100022 {'firebase_screen': '/product-popup', 'banner_id': '9152'} 9151
100076 {'firebase_screen_id': '-1531185473150377061'} Nan
100076 {'banner_id': '8567', 'firebase_screen': '/product-popup'} 8567
我正在使用以下代码。
df['banner_id'] = df['event_desc'].apply(lambda x: x.get('banner_id'))
出现错误:
'str'对象没有属性'get'
答案 0 :(得分:0)
只要它们不存储为字符串,它就可以工作:
import pandas as pd
df = pd.DataFrame([
[100013, {'firebase_screen': '/product-popup', 'banner_id': '9260'}],
[100022, {'firebase_screen': '/product-popup', 'banner_id': '9152'}],
[100076 , {'firebase_screen_id': '-1531185473150377061'}],
[100076 , {'banner_id': '8567', 'firebase_screen': '/product-popup'}]], columns =['user_id', 'event_desc'])
df['banner_id'] = df['event_desc'].apply(lambda x: x.get('banner_id'))
根据您的错误,我的猜测是列不是字典列,而是字符串列:
import pandas as pd
df = pd.DataFrame([
[100013, "{'firebase_screen': '/product-popup', 'banner_id': '9260'}"],
[100022, "{'firebase_screen': '/product-popup', 'banner_id': '9152'}"],
[100076 , "{'firebase_screen_id': '-1531185473150377061'}"],
[100076 , "{'banner_id': '8567', 'firebase_screen': '/product-popup'}"]], columns =['user_id', 'event_desc'])
df['banner_id'] = df['event_desc'].apply(lambda x: x.get('banner_id'))
AttributeError: 'str' object has no attribute 'get'
因此,您需要使用:
import pandas as pd
import ast
df = pd.DataFrame([
[100013, "{'firebase_screen': '/product-popup', 'banner_id': '9260'}"],
[100022, "{'firebase_screen': '/product-popup', 'banner_id': '9152'}"],
[100076 , "{'firebase_screen_id': '-1531185473150377061'}"],
[100076 , "{'banner_id': '8567', 'firebase_screen': '/product-popup'}"]], columns =['user_id', 'event_desc'])
df['banner_id'] = df['event_desc'].apply(lambda x: ast.literal_eval(x).get('banner_id'))
附加:
import pandas as pd
import ast
def eval_code(code):
parsed = ast.parse(code, mode='eval')
fixed = ast.fix_missing_locations(parsed)
compiled = compile(fixed, '<string>', 'eval')
return eval(compiled)
df = pd.DataFrame([
[100013, "{'firebase_screen': '/product-popup', 'banner_id': '9260'}"],
[100022, "{'firebase_screen': '/product-popup', 'banner_id': '9152'}"],
[100076 , "{'firebase_screen_id': '-1531185473150377061'}"],
[100076 , "{'banner_id': '8567', 'firebase_screen': '/product-popup'}"]], columns =['user_id', 'event_desc'])
df['banner_id'] = df['event_desc'].apply(lambda x: eval_code(x).get('banner_id'))
答案 1 :(得分:0)
df['banner_id'] = df['event_desc'].apply(lambda x: ast.literal_eval(str(x)).get('banner_id'))