I have a large table of data in pandas that could possibly be better represented as multiple relational tables. Is there a way to pick the columns to use in a new dataframe and to index the leftover columns to allow a join to recreate the original table?
For example, lets say we have a dataset where some columns get repeated multiple times over several rows:
data = {
'1': ['A', 'Abe', 'Bucket', '2022-01-01', 1,0],
'2': ['A', 'Abe', 'Bucket', '2022-01-02', 0,1],
'3': ['A', 'Abe', 'Bucket', '2022-01-03', 3,3],
'4': ['A', 'Abe', 'Bucket with Hole', '2022-01-04', 3,1],
'5': ['B', 'Ben', 'Jug', '2022-01-01', 2,1],
'6': ['C', 'Cat', 'Jug', '2022-01-01', 1,2]}
joined_df = pd.DataFrame.from_dict(data,
orient='index',
columns=['Sample', 'Author','Instrument', 'Date', 'Data1','Data2'])
joined_df
returns
Sample Author Instrument Date Data1 Data2
1 A Abe Bucket 2022-01-01 1 0
2 A Abe Bucket 2022-01-02 0 1
3 A Abe Bucket 2022-01-03 3 3
4 A Abe Bucket with Hole 2022-01-04 3 1
5 B Ben Jug 2022-01-01 2 1
6 C Cat Jug 2022-01-01 1 2
how can I go about splitting the data so that I have an "Sample" dataframe, "Time" dataframe, and "Data" dataframe:
Data:
Sample_ID Instrument_ID Date Data1 Data2
d1 s1 i1 2022-01-01 1 0
d2 s1 i1 2022-01-02 0 1
d3 s1 i1 2022-01-03 3 3
d4 s1 i2 2022-01-04 3 1
d5 s2 i3 2022-01-01 2 1
d6 s3 i3 2022-01-01 1 2
and
Samples:
Sample Name
s1 A Abe
s2 B Ben
s3 C Cat
Instument
i1 bucket
i2 Bucket with Hole
i3 Jug
Joining the sample and data dataframes on the SampleID should return the original table