df.cast({"foo": pl.Float32, "bar": pl.UInt8})
- 모든 데이터의 형식을 str(문자열)로 바꾸기 -> utf8
df = df.with_columns(pl.all().cast(pl.Utf8, strict=False))
with_columns로 모든 컬럼(pl.all())을 불러오고, 모든 컬럼의 데이터 타입을 cast로 지정해 변환함
strict는 예외발생시 강제로 수행할 것인가를 지정함
- (참고용) 리스트 안에 있는 값을 str으로 변환하는 코드
df.with_columns(pl.col("foo").cast(pl.List(pl.Utf8)))
shape: (1, 2)
┌─────────────────┬─────────────┐
│ foo | bar │
│ --- | --- │
│ list[str] | str │
╞═════════════════╪═════════════╡
│ ["1", "2", "3"] | Hello World │
└─────────────────┴─────────────┘
반응형
'파이썬(Python)&판다스(Pandas)&Polars' 카테고리의 다른 글
[polars] 특정 조건을 만족하는 데이터 조회(df.filter) (2) | 2023.11.21 |
---|---|
[polars] 셀 안의 문자열 또는 리스트 값 모두 보이게 출력(polars.Config.set_fmt_str_lengths, max_colwidth) (0) | 2023.11.21 |
[polars] write_csv로 UTF-8-SIG 처럼 저장하기(include_bom) (1) | 2023.11.21 |
[polars] with_columns(), map_elements(=apply) 컬럼 전처리 후 새로운 컬럼 만들기 (0) | 2023.11.17 |
[polars] read_csv, 특정 문자열을 None 처리, dtypes 설정 (0) | 2023.11.17 |