Home > Back-end >  How to deal with nested data in pandas dataframe via "for loop"?
How to deal with nested data in pandas dataframe via "for loop"?

Time:05-29

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

CodePudding user response:

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)
  • Related