In Pandas kann das mit df.divide gemacht werden.
Ich habe nicht herausgefunden, wie man das in polaren macht.
Ich habe das Ergebnis so erreicht:
Code: Select all
from itertools import repeat
import polars as pl
data = {'a': [i for i in range(1, 5)],
'b': [i for i in range(1, 5)],
'c': [i for i in range(1, 5)],
'd': [i for i in range(1, 5)]}
df = pl.DataFrame(data)
divisors = pl.DataFrame({'d1': 1, 'd2': 10, 'd3': 100, 'd4': 1000})
divisors_as_big_as_df = pl.concat([item for item in repeat(divisors, len(df))])
divided_df = df / divisors_as_big_as_df
print(df)
print(divisors)
print(divisors_as_big_as_df)
print(divided_df)
Code: Select all
shape: (4, 4)
┌─────┬─────┬─────┬─────┐
│ a ┆ b ┆ c ┆ d │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ 1 ┆ 1 ┆ 1 ┆ 1 │
│ 2 ┆ 2 ┆ 2 ┆ 2 │
│ 3 ┆ 3 ┆ 3 ┆ 3 │
│ 4 ┆ 4 ┆ 4 ┆ 4 │
└─────┴─────┴─────┴─────┘
shape: (1, 4)
┌─────┬─────┬─────┬──────┐
│ d1 ┆ d2 ┆ d3 ┆ d4 │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪══════╡
│ 1 ┆ 10 ┆ 100 ┆ 1000 │
└─────┴─────┴─────┴──────┘
shape: (4, 4)
┌─────┬─────┬─────┬──────┐
│ d1 ┆ d2 ┆ d3 ┆ d4 │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪══════╡
│ 1 ┆ 10 ┆ 100 ┆ 1000 │
│ 1 ┆ 10 ┆ 100 ┆ 1000 │
│ 1 ┆ 10 ┆ 100 ┆ 1000 │
│ 1 ┆ 10 ┆ 100 ┆ 1000 │
└─────┴─────┴─────┴──────┘
shape: (4, 4)
┌─────┬─────┬──────┬───────┐
│ a ┆ b ┆ c ┆ d │
│ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═════╪═════╪══════╪═══════╡
│ 1.0 ┆ 0.1 ┆ 0.01 ┆ 0.001 │
│ 2.0 ┆ 0.2 ┆ 0.02 ┆ 0.002 │
│ 3.0 ┆ 0.3 ┆ 0.03 ┆ 0.003 │
│ 4.0 ┆ 0.4 ┆ 0.04 ┆ 0.004 │
└─────┴─────┴──────┴───────┘
Gibt es also einen besseren Weg?
Mobile version