From c1d1f8004a0339fdc0713ea89b3ad61b077f57b2 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Mon, 18 Dec 2023 14:23:32 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- pandas/basics/README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pandas/basics/README.md b/pandas/basics/README.md index 6e55d31..b20de3e 100644 --- a/pandas/basics/README.md +++ b/pandas/basics/README.md @@ -783,6 +783,43 @@ E 0.243193 Name: Alpha, dtype: float64 ``` +### Order + +Replace the index:​ + +```python +New_Data = pandas.DataFrame(Data, index=New_Index)​ +``` + +Reorder the columns:​ + +```python +New_Data = pandas.DataFrame(Data, ​columns = ["ColumnName2", “ColumnName3", "ColumnName1"])​ +``` + +Reorder index and columns: + +```python +New_Data = Data.reindex(index=[0, 2, 4, 6, 8, 10, 12, 1, 3, 5, 7, 9, 11], ​columns=['ColumnName2', 'ColumnName3', 'ColumnName1'])​ +``` + +Reorder index and columns but inplace (i.e. no new variable):​ + +```python +Data.reindex(index=[0, 2, 4, 6, 8, 10, 12, 1, 3, 5, 7, 9, 11], columns=['ColumnName2', 'ColumnName3', 'ColumnName1'],​ inplace=True)​ +``` + +More index shenanigans: + +```python +New_Data = pandas.DataFrame(Data, ​columns = ["ColumnName2", “ColumnName3"], ​index=Data["ColumnName1"]) +New_Data = Data.set_index("ColumnName1") +``` + +```python +Data.set_index("ColumnName1", inplace=True)​ +``` + ## Saving (pandas.DataFrame.to_pickle) / loading (pandas.read_pickle) data ‘natively’​ Save: