How to convert values like '2+3' in a Python Pandas column to its aggregated value

会有一股神秘感。 提交于 2020-12-12 03:29:38

问题


I have a column in a DataFrame named fatalities in which few of the values are like below: data[''fatalities']= [1, 4, , 10, 1+8, 5, 2+9, , 16, 4+5]

I want the values of like '1+8', '2+9', etc to be converted to its aggregated value i.e, data[''fatalities']= [1, 4, , 10, 9, 5, 11, , 16, 9]

I not sure how to write a code to perform above aggregation for one of the column in pandas DataFrame in Python. But when I tried with the below code its throwing an error.

def addition(col):
  col= col.split('+')
  col= int(col[0]) + int(col[1])
  return col

data['fatalities']= [addition(row) for row in data['fatalities']]

Error:

IndexError: list index out of range

回答1:


Use pandas.eval what is different like pure python eval:

data['fatalities'] = pd.eval(data['fatalities'])
print (data)
  fatalities
0          1
1          4
2         10
3          9
4          5
5         11
6         16
7          9

But because this working only to 100 rows because bug:

AttributeError: 'PandasExprVisitor' object has no attribute 'visit_Ellipsis'

Then solution is:

data['fatalities'] = data['fatalities'].apply(pd.eval)



回答2:


using .map and .astype(str) to force conversion if you have mixed data types.

df['fatalities'].astype(str).map(eval)
print(df)
   fatalities
0           1
1           4
2          10
3           9
4           5
5          11
6          16
7           9


来源:https://stackoverflow.com/questions/61473292/how-to-convert-values-like-23-in-a-python-pandas-column-to-its-aggregated-val

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!