Home > Mobile >  How to map correct values?
How to map correct values?

Time:06-17

I have a dataframe where most of the values are wrongly mapped. here is my dataframe.

df:

 Name           Age           Cust_Id
 alex           47            1923894833

I need to re-map every values to its correct column.

df_output:

 Name           Age           Phn_No
 alex           47            1923894833

CodePudding user response:

For fun, here is a hack way to perform the task (I really wouldn't use this in real-life):

df.apply(lambda row:
         pd.Series(sorted(row, key=lambda x: (not str(x).isalpha())*(1 (len(str(x))>2))),
                   index=row.index), axis=1)

How it works:

  • convert as string
    • if all letters -> 0
    • if length > 2 -> 2
    • else 1
  • use the above number to sort the values and generate a new Series
    • first field will be all letters, second 2 characters, third the longer string

output:

    Name Age       Phn_No
0   alex  47   1923894833
1   Ross  23  17293883222
2   mike  34   8738272882
3  stefy  39  19298388392
  • Related