0

I am converting the following json string into dataframe. A list of keys is supposed to be unique and represent a primary key column values. For col1 and col2 we also have values set.

{"col1": {"0": col1val0, "1": col1val1}, "col2": {"0" : col2val0, "1": col2val1}, "index": {"0": ["key1","key2",""], "1": ["key1","key2","key3"]}}

using

df = pd.read_json(json_string)

Question about my index - it is a dict in its original form but when converted to df, it becomes a part of the index that I can't seem to extract.

                index         col1        col2
0        [key1,key2,]     col1val0    col2val0
1    [key1,key2,key3]     col1val1    col2val1

I tried to copy index into a new column, transposing df, tried getting shape of the index - it only returns numerical part - {0,1} and shows up as Int64Index type despite showing [key1,key2,] part of the index when printed. How can I extract it? Preferably I'd like to create primary key columns pk1, pk2, pk3 and populate them with these values.

1
  • What's your expected output? Commented Mar 15, 2021 at 13:49

1 Answer 1

0

You can split index into columns by converting the values to list (making it a list of lists) and assigning those values to the respective columns:

df[['pk1', 'pk2', 'pk3']] = df['index'].to_list()

df

Output:

       col1      col2               index   pk1   pk2   pk3
0  col1val0  col2val0      [key1, key2, ]  key1  key2      
1  col1val1  col2val1  [key1, key2, key3]  key1  key2  key3
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the tip! It didn't convert to list as expected but stack gave me enough to be able to determine the type of object and convert it using values.tolist() ('Series' object has no attribute 'to_list')
@cleanoxi Cool, but Series does have to_list method, does it not? pandas.pydata.org/pandas-docs/stable/reference/api/…
Ah, maybe you're on an older version of pandas... stackoverflow.com/questions/57854228/…
Definitely on the older version - that explains it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.