I have two dataframes, one of them is just a single row, and I would like to transform each of the columns in the first one with the values in the single row in some fashion. How do I do this? Here's what I want to achieve:
df1 = pl.DataFrame({'c1': [2,4,6],'c2': [20,40,60],'c3': [10,20,30]})
df2 = pl.DataFrame({'c1': [2],'c2': [20],'c3': [10]})
df = df.select([
pl.col('c1')/df2['c1'],
pl.col('c2')/df2['c2'],
pl.col('c3')/df2['c3'],
])
Now, imagine I have hundreds of columns. Above code doesn't scale, how do I do this best? Thanks!
If df2
is guaranteed to be a single row AND the names from df1 and df2 will always match then you can do:
df1.select(pl.col(x)/df2[x] for x in df1.columns)
If df2 is more than a single row or if the name in df1 don't exist in df2 then this will error out.