简体   繁体   English

从列中的字典获取值。 AttributeError: 'NoneType' object 在值为“None”时没有属性“get”

[英]Get values from a dict in an column. AttributeError: 'NoneType' object has no attribute 'get' when a value is "None"

I have this df我有这个df

     simbolo                                                     puntas           ultimoPrecio  
10   AE38             {'cantidadCompra': 79668.0, 'precioCompra': 60...           6080.00   
11   AL41C                                                         None             36.50   

and I get the first value from dict in "puntas" column.我从“puntas”列中的字典中获得第一个值。

p['Cant_Compra']=[x.get('cantidadCompra',0) for x in p['puntas']]

output: output:

     simbolo Cant_Compra  ultimoPrecio  
10    AE38     79668.0         6080.00 

but if value is None as line 11 appears next error message:但如果值为None ,则第 11 行会出现下一条错误消息:

AttributeError: 'NoneType' object has no attribute 'get' AttributeError: 'NoneType' object 没有属性 'get'

what can I do?我能做什么?

import ast
from io import StringIO

# sample data
s = """simbolo|puntas|ultimoPrecio  
AE38|{'cantidadCompra': 79668.0, 'precioCompra': 60}|6080.00   
AL41C|None|36.50"""
df = pd.read_csv(StringIO(s), sep='|')
df['puntas'] = df['puntas'].apply(ast.literal_eval)

# convert the dict column to a dataframe and assign the values to your column
df['Cant_Compra'] = pd.DataFrame(df['puntas'].to_dict()).T['cantidadCompra']


  simbolo                                           puntas  ultimoPrecio    \
0    AE38  {'cantidadCompra': 79668.0, 'precioCompra': 60}          6080.0   
1   AL41C                                             None            36.5   

  Cant_Compra  
0       79668  
1        None 

this works:这有效:

def fun(x):
  if x==None:
    return None
  return x['cantidadCompra']
p['Cant_Compra']=p['puntas'].apply(func=fun)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'NoneType' object 没有属性 'get' - 当我有数据并且没有字段为 None 时如何解决? - AttributeError: 'NoneType' object has no attribute 'get' - How to solve it when I even have the data and no field is None? AttributeError: 'NoneType' object 没有属性 'get_values' - AttributeError: 'NoneType' object has no attribute 'get_values' AttributeError:当我保存在age中时,“ NoneType”对象没有属性“ get” - AttributeError: 'NoneType' object has no attribute 'get' when I save in amage AttributeError: 'NoneType' object 在写入文件时没有属性 'get' - AttributeError: 'NoneType' object has no attribute 'get' when writing into a file Flasgger AttributeError:'NoneType'对象没有属性'get'吗? - Flasgger AttributeError: 'NoneType' object has no attribute 'get'? AttributeError: 'NoneType' object 没有属性 'get' helpp - AttributeError: 'NoneType' object has no attribute 'get' helpp 'NoneType' object 没有属性 'get':AttributeError - 'NoneType' object has no attribute 'get': AttributeError 'AttributeError:'NoneType'对象没有属性'to_dict' - 'AttributeError: 'NoneType' object has no attribute 'to_dict' AttributeError: 'NoneType' 对象在使用 NetworkX 的 greedy_modularity_communities 时没有属性 'get' - AttributeError: 'NoneType' object has no attribute 'get' when using greedy_modularity_communities from NetworkX AttributeError: 'NoneType' object 没有属性 'get' - get.("href") - AttributeError: 'NoneType' object has no attribute 'get' - get.("href")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM