English 中文(简体)
Python-polar 现有两个列的串联
原标题:python-polars string concatenation of two existing columns
I want to concatenate the last letter from two existing columns and create a new column from this using polars.LazyFrame for example in pandas can achieve this with the following code import pandas as pd df = pd.DataFrame({"col1":["abc","def"], "col2":["ghi","jkl"]}) df["last_letters_concat"]=df["col1"].str.strip().str[-1]+df["col2"].str.strip().str[-1] print(df) col1 col2 last_letters_concat 0 abc ghi ci 1 def jkl fl My attempt in polars import polars as pl from polars import col #using same df df.lazy().with_columns( (pl.col("col1")[-1] + pl.col( col2 ))[-1].alias("last_letters_concat") ).collect() How can i do this?
最佳回答
You can use the str.slice expression for that. Below I show to examples that produce the same result. df = pl.DataFrame({ "col1": ["abc","def"], "col2":["ghi","jkl"] }) # concat all last letters out1 = df.select( pl.concat_str(pl.col("col1").str.slice(-1), pl.col("col2").str.slice(-1)) ) # concat only two specific columns out2 = df.select( pl.col("col1").str.slice(-1) + pl.col("col2").str.slice(-1) ) assert out1.equals(out2) print(out1) shape: (2, 1) ┌──────┐ │ col1 │ │ --- │ │ str │ ╞══════╡ │ ci │ │ fl │ └──────┘ I recommend using the concat_str expression as this has O(n) complexity where n is the number of columns you add, whereas the addition operator has O(n^2) complexity. EDIT: as of polars >= 0.14.12 the optimizer will ensure it always is linear complexity
问题回答

暂无回答




相关问题
Map user-defined function on multiple polars columns

I am doing a bit of data munging on a polars.Dataframe and I could write the same expression twice, but I would ideally like to cut down on that a bit. So I was thinking that I could just create a ...

Python Polars - conditional join on value between other columns

I have a Polars DataFrame that looks like this: ┌────────────┬───────┐ │ date ┆ value │ │ --- ┆ --- │ │ str ┆ i64 │ ╞════════════╪═══════╡ │ 2022-01-01 ┆ 3 │ ├╌╌╌╌╌╌╌╌╌╌╌╌┼╌...

How to use polars dataframes with scikit-learn?

I m unable to use polars dataframes with scikitlearn for ML training. Currently I m doing all the dataframe preprocessing in polars and during model training i m converting it into a pandas one in ...

Polars: Nesting `over` calls

Context. I have written a function that computes the mean of all elements in a column except the elements in the current group. df = pl.DataFrame({ "group": ["A", "A",...

Storing in PostgreSQL using Python Polars

I want to store a datframe from a parquet file into a PostgreSQL using Polars using this code: def store_in_postgresql(df): password = anon username = postgres database = nyc_taxis ...

热门标签