Nested डेटा कॉलम से निपटना

pandas के साथ डेटा को रीशेप करना

Maria Eugenia Inzaugarat

Data Scientist

समीक्षा

  • json_normalize() का उपयोग करके nested JSON को DataFrame में कैसे पढ़ें.
pandas के साथ डेटा को रीशेप करना

कॉलम में nested डेटा

writers = ["Mary Shelley", "Ernest Hemingway"]
books = ["{'title': 'Frankenstein', 'year': 1818}", 
        "{'title': 'The Old Man and the Sea', 'year': 1951}"]

collection = pd.DataFrame( )
pandas के साथ डेटा को रीशेप करना

कॉलम में nested डेटा

writers = ["Mary Shelley", "Ernest Hemingway"]
books = ["{'title': 'Frankenstein', 'year': 1818}", 
        "{'title': 'The Old Man and the Sea', 'year': 1951}"]

collection = pd.DataFrame(dict( ))
pandas के साथ डेटा को रीशेप करना

कॉलम में nested डेटा

writers = ["Mary Shelley", "Ernest Hemingway"]
books = ['{"title": "Frankenstein", "year": "1818"}',
         '{"title": "The Old Man and the Sea", "year":"1951"}']

collection = pd.DataFrame(dict(writers=writers, books=books))
collection
            writers                                               books
0      Mary Shelley             {'title': 'Frankenstein', 'year': 1818}
1  Ernest Hemingway  {'title': 'The Old Man and the Sea', 'year': 1951}
pandas के साथ डेटा को रीशेप करना

Nested डेटा कन्वर्ट करना

import json

books = collection['books']
pandas के साथ डेटा को रीशेप करना

Nested डेटा कन्वर्ट करना

import json

books = collection['books'].apply( )
pandas के साथ डेटा को रीशेप करना

Nested डेटा कन्वर्ट करना

import json

books = collection['books'].apply(json.loads)
pandas के साथ डेटा को रीशेप करना

Nested डेटा कन्वर्ट करना

import json

books = collection['books'].apply(json.loads).apply(pd.Series)
books
                     title  year
0             Frankenstein  1818
1  The Old Man and the Sea  1951
pandas के साथ डेटा को रीशेप करना

फिर से concatenate करें

collection = collection.drop(columns='books')

pd.concat([collection, books], axis=1)
            writers                    title  year
0      Mary Shelley             Frankenstein  1818
1  Ernest Hemingway  The Old Man and the Sea  1951
pandas के साथ डेटा को रीशेप करना

Nested डेटा को dump करना

import json
books = collection['books'].apply(json.loads)



pandas के साथ डेटा को रीशेप करना

Nested डेटा को dump करना

import json
books = collection['books'].apply(json.loads).to_list()

books_dump = json.dumps(books)
new_books = pd.read_json(books_dump)
new_books
                     title  year
0             Frankenstein  1818
1  The Old Man and the Sea  1951
pandas के साथ डेटा को रीशेप करना

Nested डेटा को dump करना

pd.concat([collection['writers'], new_books], axis=1)
            writers                    title  year
0      Mary Shelley             Frankenstein  1818
1  Ernest Hemingway  The Old Man and the Sea  1951
pandas के साथ डेटा को रीशेप करना

अभ्यास करते हैं!

pandas के साथ डेटा को रीशेप करना

Preparing Video For Download...