0

I have got a nested data in pandas dataframe and I want to flatten the column, "names" by using "pd.Dataframe ()" function. When I attempt to flatten via "for loop" it produces 5 different dataframe list, which I do not expect to have and rather only one dataframe list with all values listed. I have already tried "concat" or "append" methods but it did not give any clue to move forward. Any help/comment is welcome, thanks so much. Here is my "for loop":

            x=df['names'].iloc[0:4]
                     
            name_data = pd.DataFrame(x)

           
            data_row=[]
            for data in x:
                data_row =pd.DataFrame(data)
        
                st.write(data_row)

nested pandas dataframe

1 Answer 1

1

If I understand correctly, you want to concat the 5 tables in the example images above to only 1 table and show the result table on streamlit.

All you have to do are

  • change from data_row =pd.DataFrame(data) to data_row += [pd.DataFrame(data)]
  • After loop for loop finished
    • you can concat all dataframes in data_row to one dataframe by using data_row = pd.concat(data_row)
    • and then, show the result table with streamlit by using st.write(data_row)

Here is example for tackling your problem.

df = pd.DataFrame({
    'names': [[{'name':'a'},{'name':'b'}], [{'name':'c'}]]
})

x=df['names'].iloc[0:2]

data_row = []
for data in x:
    data_row += [pd.DataFrame(data)]
data_row = pd.concat(data_row)

st.write(data_row)

or you can create the list of dictionary and create dataframe by using the example below

data_row = []
for data in x:
    data_row += data
data_row = pd.DataFrame(data_row)
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.