0

I am trying to write something similar to a CASE STATEMENT for Python, so my aim is to check whether the column is a list, and when True it should explode that column.

Static Version:

docs = json.loads(docs)
docs = json_normalize(docs).explode("values")

Dynamic Version (How I started it, but then couldn't figure out how to finish it, or if it's even the right way to do it ):

x = (docs.applymap(type) == list).all()
for i in x:
    np.where(i == True,......)

Any guidance or tips will be greatly appreciated.

2
  • In fact, explore with not list type columns is OK. Commented Feb 10, 2021 at 9:05
  • check after apply explore to every column, if the row num is not changed, then it's the end. Commented Feb 10, 2021 at 9:09

1 Answer 1

1

I Figured out how to dynamically explode a column:

docs = json.loads(docs)
docs = json_normalize(docs)
x = (docs.applymap(type) == list).all()
y = x.index[x].tolist()
for i in y:
    docs = docs.explode(i)
print(docs)

This checks whether the column is a list, if its True, it iterates over the column names and explodes each one.

Sign up to request clarification or add additional context in comments.

Comments

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.