Reshape DataFrame¶
In [6]:
import pandas as pd
import numpy as np
In [7]:
df1 = pd.DataFrame({"x": [1, 2, 3], "y": [5, 4, 3]})
df1
Out[7]:
In [8]:
df2 = pd.DataFrame({"x": [0, 0, 3000], "y": [78, 4, 3]})
df2
Out[8]:
Melt¶
In [24]:
df = pd.DataFrame(
{"id": ["a", "b", "c"], "x": [1, 3, 5], "y": [2, 4, 6], "z": [7, 8, 9]}
)
df
Out[24]:
In [26]:
dfm = pd.melt(df, id_vars="id", value_vars=["x", "y", "z"])
dfm
Out[26]:
Stack¶
In [52]:
df = pd.DataFrame(
{"id": ["a", "b", "c"], "x": [1, 3, 5], "y": [2, 4, 6], "z": [7, 8, 9]}
)
df
Out[52]:
In [57]:
x = df.stack()
x
Out[57]:
In [58]:
x.unstack()
Out[58]:
In [60]:
type(x)
Out[60]:
Insert Columns - pandas.DataFrame.insert¶
In [3]:
df = pd.DataFrame(
{"id": ["a", "b", "c"], "x": [1, 3, 5], "y": [2, 4, 6], "z": [7, 8, 9]}
)
df
Out[3]:
In [4]:
df.insert(0, "col", 0)
In [5]:
df
Out[5]:
reshape¶
In [ ]:
overlap.spot_sample_detail_id.reshape(100, 3)
rename¶
In [10]:
df = pd.DataFrame(
{"id": ["a", "b", "c"], "x": [1, 3, 5], "y": [2, 4, 6], "z": [7, 8, 9]}
)
df
Out[10]:
In [13]:
df.rename(columns=df.iloc[0])
Out[13]:
In [11]:
help(df.rename)
In [ ]: