I have a dataframe with person names with these fields – last, first and middle names, i’m trying to concatenating these fields to get a full_name column in a dataframe as below.
JavaScript
x
8
1
dfl.with_columns(
2
pl.concat_str([pl.col('last_name'),
3
pl.col('first_name_or_initial'),
4
pl.col('middle_name_or_initial')],' ').alias('full_name')).select([
5
pl.col('full_name'),pl.col('last_name'),
6
pl.col('first_name_or_initial'),
7
pl.col('middle_name_or_initial')])
8
Here is the output:
Why I’m getting null in full_name after concatenating last, first and middle_names ? Here If any of field holds null while concatenating strings the result will be NULL like wise in above example last_name is Lee, first_name is James, middle_names is NULL hence full_name is NULL how to ignore NULL field when concatenating the fields.
Advertisement
Answer
We can apppend fill_null
to the Expressions passed to concat_str
:
JavaScript
1
18
18
1
dfl.with_columns(
2
pl.concat_str(
3
[
4
pl.col("last_name").fill_null(""),
5
pl.col("first_name_or_initial").fill_null(""),
6
pl.col("middle_name_or_initial").fill_null(""),
7
],
8
" ",
9
).str.rstrip().alias("full_name")
10
).select(
11
[
12
pl.col("full_name"),
13
pl.col("last_name"),
14
pl.col("first_name_or_initial"),
15
pl.col("middle_name_or_initial"),
16
]
17
)
18
JavaScript
1
18
18
1
shape: (5, 4)
2
┌─────────────────────┬────────────┬───────────────────────┬────────────────────────┐
3
│ full_name ┆ last_name ┆ first_name_or_initial ┆ middle_name_or_initial │
4
│ --- ┆ --- ┆ --- ┆ --- │
5
│ str ┆ str ┆ str ┆ str │
6
╞═════════════════════╪════════════╪═══════════════════════╪════════════════════════╡
7
│ Dionne Thomas Roger ┆ Dionne ┆ Thomas ┆ Roger │
8
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
9
│ Lee James ┆ Lee ┆ James ┆ null │
10
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
11
│ Lee James ┆ Lee ┆ James ┆ null │
12
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
13
│ Latella Domenic C. ┆ Latella ┆ Domenic ┆ C. │
14
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
15
│ Trumpauska Arvydas ┆ Trumpauska ┆ Arvydas ┆ null │
16
└─────────────────────┴────────────┴───────────────────────┴────────────────────────┘
17
18
I’ve filled the null
values with an empty string ""
, but you can choose whatever value you need.