Reputation: 417
I have a polars dataframe and I want to set the first row as header. I thought about renaming the column name one by one with the value of the first row of the correspondent column. How can I do this in polars?
Upvotes: 0
Views: 4709
Reputation: 18446
Another way to do this is:
df.columns=df.iter_rows().__next__()
iter_rows
makes a generator so it's not going to read any more of the df than the first row. If you don't like using the internal __next__
then you could do
df.columns=df.head(1).rows()[0]
or
for row in df.iter_rows():
df.columns=row
break
or
rows=df.iter_rows()
df.columns=next(rows)
Upvotes: 0
Reputation: 21279
[UPDATE]: @kirara0048's suggestion of .to_dicts()
is a much simpler approach.
>>> df.head(1).to_dicts().pop()
{'column_0': 'one', 'column_1': 'two', 'column_2': 'three'}
Which can be passed directly to .rename()
df.rename(df.head(1).to_dicts().pop())
Perhaps there is a more direct method but you could take the first row and .transpose().to_series()
>>> df = pl.DataFrame([["one", "four"], ["two", "five"], ["three", "six"]])
>>> df.head(1).transpose().to_series()
shape: (3,)
Series: 'column_0' [str]
[
"one"
"two"
"three"
]
This can be used to create a dict of old: new
and passed to .rename()
>>> df.rename(dict(zip(df.columns, df.head(1).transpose().to_series())))
shape: (2, 3)
┌──────┬──────┬───────┐
│ one | two | three │
│ --- | --- | --- │
│ str | str | str │
╞══════╪══════╪═══════╡
│ one | two | three │
├──────┼──────┼───────┤
│ four | five | six │
└──────┴──────┴───────┘
.slice(1)
can be used to "remove" the first row if desired:
>>> df.rename(dict(zip(df.columns, df.head(1).transpose().to_series()))).slice(1)
shape: (1, 3)
┌──────┬──────┬───────┐
│ one | two | three │
│ --- | --- | --- │
│ str | str | str │
╞══════╪══════╪═══════╡
│ four | five | six │
└──────┴──────┴───────┘
You can also assign to .columns
- I'm unsure if this is considered "bad style" or not.
>>> df.columns = df.head(1).transpose().to_series()
>>> df
shape: (2, 3)
┌──────┬──────┬───────┐
│ one | two | three │
│ --- | --- | --- │
│ str | str | str │
╞══════╪══════╪═══════╡
│ one | two | three │
├──────┼──────┼───────┤
│ four | five | six │
└──────┴──────┴───────┘
Upvotes: 2