Pandas 计算数据框中列本身的数据

Pandas computing the data inside the column itself in dataframe

I have come up with a problem where my data in the column has been recorded as 90-2,91-3,90+4 etc.My motive here is to add and subtract the data directly into the column itself. Datatype of the column is an object.

df = df1["ldm"].str.split('+',expand =True)

if df.shape[1]>1:
  df_2 = df[0].str.split('-',expand = True)
  df_2 = df_2.fillna(value=0)
  df = df.fillna(value=0)
  df_2[0] = df_2[0].astype(int)
  df[1] = df[1].astype(int)
  df_2[1] = df_2[1].astype(int)
  df_2['3'] = df[1]
  df_2[0]=df_2[0]-df_2[1]
  df_2[0] = df_2[0]+df_2['3']

df1['ldm'] = df_2[0]

这是我的低效解决方案。我正在寻找一种在数据框中计算它的有效方法。

使用pandas.eval。它支持的操作范围有限,使用起来比python的eval更安全,比ast.literal_eval.

更方便

来自文档:

The following arithmetic operations are supported: +, -, *, /, **, %, // (python engine only) along with the following boolean operations: | (or), & (and), and ~ (not). Additionally, the 'pandas' parser allows the use of and, or, and not with the same semantics as the corresponding bitwise operators. Series and DataFrame objects are supported and behave as they would with plain ol’ Python evaluation.

df['ldm2'] = pd.eval(df['ldm'])

输出:

    ldm  ldm2
0  90-2    88
1  91-3    88
2  90+4    94