1

I have a list of lists which also contains headers. Headers don't match with all the lists and not all the lists have the same count. Something like this:

[
[['Row', '1'], ['header1', '1.23'], ['header2','5.67'], ['header3','6.55']],
[['Row', '2'], ['header2','19.67'], ['header3','9.55']],
[['Row', '3'], ['header2','19.67'], ['header3','9.55'], ['header4','16.88']]
]

I would like to convert it to a CSV with all header values filled and missing header values filled with "N/A"

like this:

Row, Header1, Header2, Header3, Header4
1, 1.23, 5.67, 6.55, N/A
2, N/A, 19.67, 9.55, N/A
3, N/A, 19.67, 9.55, 16.88

I was wondering how to go about doing this

0

1 Answer 1

1

You can convert your datastructure into a list of dictionaries. Then you can use pandas.DataFrame.from_records:

import pandas as pd

data = [
[['Row', '1'], ['header1', '1.23'], ['header2','5.67'], ['header3','6.55']],
[['Row', '2'], ['header2','19.67'], ['header3','9.55']],
[['Row', '3'], ['header2','19.67'], ['header3','9.55'], ['header4','16.88']]
]

df = pd.DataFrame.from_records(map(dict, data)).set_index("Row").fillna("N/A")
#     header1 header2 header3 header4
# Row                                
# 1      1.23    5.67    6.55     N/A
# 2       N/A   19.67    9.55     N/A
# 3       N/A   19.67    9.55   16.88
df.to_csv("file.csv")
Sign up to request clarification or add additional context in comments.

2 Comments

I actually have a list of lists. I made the error of separating values with a : when I posted the question.
@PuppyHeadedNinja: Yeah, I fixed it so it first converts it to a list of dicts

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.