Home > database >  Pandas computing the data inside the column itself in dataframe
Pandas computing the data inside the column itself in dataframe

Time:03-26

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]

This is my inefficient solution..I am looking for an efficient way to compute this in the dataframe.

CodePudding user response:

Use pandas.eval. It supports a limited range of operations, which makes it much safer to use than python's eval and more convenient than ast.literal_eval.

From the documentation:

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'])

output:

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