Home > Software engineering >  how to move columns in pandas
how to move columns in pandas

Time:05-06

I have pyarrow table with header like that: ['column1','column2','column3','column4','column5' ] I want to swap and mode column header and data: ['column1','column2','column5','column3','column4' ] How I can do it with pandas or pyarrow

CodePudding user response:

df = df[['column1','column2','column5','column3','column4' ]]

This would rearrange the columns

CodePudding user response:

In PyArrow you can try with using select() method:

import pyarrow as pa

# Define an example pa.Table
n_legs = pa.array([2, 4, 5, 100])
animals = pa.array(["Flamingo", "Horse", "Brittle stars", "Centipede"])
names = ["n_legs", "animals"]
table = pa.table([n_legs, animals], names=names)

# Select columns
table.select([1,0])

You would get:

>>> table.select([1,0])
pyarrow.Table
animals: string
n_legs: int64
----
animals: [["Flamingo","Horse","Brittle stars","Centipede"]]
n_legs: [[2,4,5,100]]

vs the original:

>>> table
pyarrow.Table
n_legs: int64
animals: string
----
n_legs: [[2,4,5,100]]
animals: [["Flamingo","Horse","Brittle stars","Centipede"]]

You can also use custom schema when converting Pandas dataframe to PyArrow table:

import pyarrow as pa
import pandas as pd

# Define Pandas dataframe
df = pd.DataFrame({'year': [2020, 2022, 2019, 2021],
                   'n_legs': [2, 4, 5, 100],
                   'animals': ["Flamingo", "Horse", "Brittle stars", "Centipede"]})

# Define custom PyArrow schema
my_schema = pa.schema([
    pa.field('n_legs', pa.int64()),
    pa.field('animals', pa.string()),
    pa.field('year', pa.int64())
])

# Read Pandas dataframe as PyArrow table with specified schema
table = pa.table(df, my_schema)

You would get:

>>> table.to_pandas()
   n_legs        animals  year
0       2       Flamingo  2020
1       4          Horse  2022
2       5  Brittle stars  2019
3     100      Centipede  2021
>>> df
   year  n_legs        animals
0  2020       2       Flamingo
1  2022       4          Horse
2  2019       5  Brittle stars
3  2021     100      Centipede

CodePudding user response:

In pyarrow you can do it this way:

columns = ['column1', 'column2']
table.select(columns)
  • Related