1

In pandas when I am using df.apply(someFun,axis=1) which goes to a function in which a list of zipped values i.e. list(zip(val1,val2)) is being returned. And then df.explode() is used.

But in polars when using df.with_columns((pl.Series(list(someFun(x)) for x in df.iter_rows(named=True)).alias(column1))).

It is creating a hexadecimal values in Column1.

using pandas:

self.var1 = 'A'
self.var2 = 'B'

def someFun(row):
    var  = self.var1
    var2 = self.var2
    cal1 = row[var] + row[var2]
    cal2 = row[var] * row[var2]
    return list(zip(cal1 , cal2))

df["column1"] = df.apply(someFun,axis=1)
exploded_df = df.explode("column1",ignore_index=True)

in polars dataframe value in column1 is like b"\x00O\, and explode is throwing error on this column.

tried converting the hexadecimal value to list but can't update it back in the dataframe so that explode function can work.

  • 3
    Can give a runnable code example that creates hexadecimal values? i.e. the real `someFun` – jqurious Jul 31 '23 at 10:58
  • your `someFun` is using numbered indices not named ones so why are you using `iter_rows(named=True)` – Dean MacGregor Jul 31 '23 at 11:25
  • 1
    yes I have done it for the example, initially it is using named indexing. I have updated the example. – dhruv bothra Jul 31 '23 at 12:33
  • We still cannot run your example unfortunately, there are no `self` or `df` definitions. The example function doesn't really help either as it doesn't produce the `b''` strings you mention. You'll have to give us code that allows us to run your `df.with_columns()` line to produce the same error. – jqurious Jul 31 '23 at 14:17
  • I've voting to close. This question is eerily similar to [this](https://stackoverflow.com/questions/76788887/polars-apply-function-dosent-pass-column-name-to-function). Seems like a bot or something. – Dean MacGregor Aug 01 '23 at 15:02
  • Does this answer your question? [Polars apply function dosen't pass column name to function](https://stackoverflow.com/questions/76788887/polars-apply-function-dosent-pass-column-name-to-function) – michael_s Aug 11 '23 at 10:52
  • @michael_s no i noticed that both the values cal1 and cal2 are of different data types like int and str. casted both in str and than apply zip(), and it worked for me – dhruv bothra Aug 12 '23 at 09:37

0 Answers0