0

I have a .txt file, named 'data.txt' which is a list of dictionaries, i.e., something like,

{"958464": ["item1", "item3"], "917654": [], "1347584": ["item2", "item5", "item6", "item7"], "914082": [], "946178": ["item1", "item4", "item15"], ...}

How can form a dataframe, with the above file?

My idea is cross information (about ID) with another database, so I think form dataframe like,

      ID            ITEMS
0    958464    "item1", "item3"
1    917654
2    1347584   "item2", "item5", "item6", "item7"
3    914082
4    946178    "item1", "item3", "item5"
...

Or maybe, create a dataframe like

      ID        Item1   item2   item3   ...
0    958464       1      0        1
1    917654       0      0        0
2    1347584      0      1        0   
3    914082       0      0        0
4    946178       1      0        1
...

I tried

data = pd.read_csv('data1.txt')
pd.DataFrame(data.items())
1

1 Answer 1

1

Try this :

k = {"958464": ["item1", "item3"], "917654": [],
 "1347584": ["item2", "item5", "item6", "item7"], "914082": [], "946178": ["item1", "item4", "item15"]}

import pandas

df1=pandas.DataFrame.from_dict(k,orient='index')
print(df1)

O/P :

             0      1       2      3
958464   item1  item3    None   None
917654    None   None    None   None
1347584  item2  item5   item6  item7
914082    None   None    None   None
946178   item1  item4  item15   None
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.