Home > Net >  Converting column value to ascii
Converting column value to ascii

Time:11-19

I have a data frame with columns, say v1~v4

| _NAME    | _TIMESTAMP          | v0    | v1   | v2    | v3    | v4    |
|----------|---------------------|-------|------|-------|-------|-------|
| BRAKE_LH | 17-11-2021 22:50:43 | 13896 | 8262 | 12339 | 13110 | 13107 |
| BRAKE_LH | 17-11-2021 22:51:34 | 13896 | 8262 | 12339 | 13110 | 13107 |
| BRAKE_LH | 17-11-2021 22:51:35 | 13896 | 8262 | 12339 | 13110 | 13107 |
| BRAKE_LH | 17-11-2021 22:51:36 | 13896 | 8262 | 12339 | 13110 | 13107 |
| BRAKE_LH | 17-11-2021 22:51:37 | 0     | 0    | 0     | 0     | 0     |  

If I want to do the below function to the columns v1~v4

df['v0'] = df['v0'].apply(lambda x: chr(round(x / 256))   chr(x % 256)).apply(lambda x: x[::-1])
df['v1'] = df['v1'].apply(lambda x: chr(round(x / 256))   chr(x % 256)).apply(lambda x: x[::-1])
df['v2'] = df['v2'].apply(lambda x: chr(round(x / 256))   chr(x % 256)).apply(lambda x: x[::-1])
df['v3'] = df['v3'].apply(lambda x: chr(round(x / 256))   chr(x % 256)).apply(lambda x: x[::-1])
df['v4'] = df['v4'].apply(lambda x: chr(round(x / 256))   chr(x % 256)).apply(lambda x: x[::-1])

In come cases the columns goes beyond 4 columns, say 40 or 100 columns

Is there a simple way to apply it for all columns, except--> _NAME & _TIMESTAMP columns

CodePudding user response:

You can set columns _NAME and _TIMESTAMP as index (to exclude them for processing) by .set_index(). Then use .applymap() to use your formulas for processing elementwise on each column. Finally, restore the columns _NAME and _TIMESTAMP to data columns by .reset_index(), as follows:

df.set_index(['_NAME', '_TIMESTAMP']).applymap(lambda x: chr(round(x / 256))   chr(x % 256)).applymap(lambda x: x[::-1]).reset_index()

Result:

      _NAME           _TIMESTAMP  v0  v1  v2  v3  v4
0  BRAKE_LH  17-11-2021 22:50:43  H6  F   30  63  33
1  BRAKE_LH  17-11-2021 22:51:34  H6  F   30  63  33
2  BRAKE_LH  17-11-2021 22:51:35  H6  F   30  63  33
3  BRAKE_LH  17-11-2021 22:51:36  H6  F   30  63  33
4  BRAKE_LH  17-11-2021 22:51:37  

CodePudding user response:

You can put the columns you want to ignore in a set, IGNORELIST = {'_NAME', '_TIMESTAMP'}.

Then iterate through the column names and check whether a name is ignored or not. If it's not, apply your functions.

here's an example

# df = ..your dataframe..

IGNORELIST = {'colname1', 'colname2'}

for colname in df.columns:
  if not colname in IGNORELIST:
    df[colname] = df[colname].apply(lambda x: chr(round(x / 256))   chr(x % 256)).apply(lambda x: x[::-1])
  • Related