नेस्टेड डेटा को DataFrame में पढ़ना

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

Maria Eugenia Inzaugarat

Data Scientist

रीव्यू

  • DataFrame और Series को reshape करें
  • कॉलम में मौजूद सूचियों को explode करें
  • स्ट्रिंग्स को split और concatenate करें
pandas के साथ डेटा को रीशेप करना

JSON फॉर्मेट

  • JavaScript Object Notation
  • डेटा-इंटरचेंज फॉर्मेट
  • मनुष्यों के लिए पढ़ना-लिखना आसान
  • मशीनों के लिए parse और generate करना आसान
pandas के साथ डेटा को रीशेप करना

JSON फॉर्मेट

my_writer
{
  "first" : "Mary",
  "last" : "Shelley",
  "country" : "England",
  "books" : 12
}
pandas के साथ डेटा को रीशेप करना

नेस्टेड JSON

writers
writers = [
            {
              "first": "Mary",
              "last": "Shelley",
              "books": {"title": "Frankenstein", "year": 1818}
            },
            {
              "first": "Ernest",
              "last": "Hemingway",
              "books": {"title": "The Old Man and the Sea", "year": 1951}
            }
          ]
pandas के साथ डेटा को रीशेप करना

डेटा normalization

from pandas import json_normalize
json_normalize(writers)
    first       last              books.title  books.year
0    Mary    Shelley             Frankenstein        1818
1  Ernest  Hemingway  The Old Man and the Sea        1951
pandas के साथ डेटा को रीशेप करना

डेटा normalization

writers_norm = json_normalize(writers, sep='_')
writers_norm
    first       last              books_title  books_year
0    Mary    Shelley             Frankenstein        1818
1  Ernest  Hemingway  The Old Man and the Sea        1951
pandas के साथ डेटा को रीशेप करना

डेटा normalization

pd.wide_to_long(writers_norm, stubnames=['books'], i=['first', 'last'], j='feature', sep='_', suffix='\w+')
                                            books
first  last      feature                         
Mary   Shelley   title               Frankenstein
                 year                        1818
Ernest Hemingway title    The Old Man and the Sea
                 year                        1951
pandas के साथ डेटा को रीशेप करना

कॉम्प्लेक्स JSON

writers
[
    {'name': 'Mary',
     'last': 'Shelley',
     'books': [{'title': 'Frankestein', 'year': 1818},
                {'title': 'Mathilda ', 'year': 1819},
                {'title': 'The Last Man', 'year': 1826}]},
    {'name': 'Ernest',
     'last': 'Hemmingway',
     'books': [{'title': 'The Old Man and the Sea', 'year': 1951},
               {'title': 'The Sun Also Rises', 'year': 1927}]}
]
pandas के साथ डेटा को रीशेप करना

कॉम्प्लेक्स JSON

json_normalize(writers)
     name        last                                              books
0    Mary     Shelley  [{'title': 'Frankestein', 'year': 1818}, {'tit...
1  Ernest  Hemmingway  [{'title': 'The Old Man and the Sea', 'year': ...
pandas के साथ डेटा को रीशेप करना

Record path

json_normalize(writers, record_path='books')
                     title  year
0              Frankestein  1818
1                Mathilda   1819
2             The Last Man  1826
3  The Old Man and the Sea  1951
4       The Sun Also Rises  1927
pandas के साथ डेटा को रीशेप करना

मेटाडेटा

json_normalize(writers, record_path='books', meta=['name', 'last'])
                     title  year    name        last
0              Frankestein  1818    Mary     Shelley
1                Mathilda   1819    Mary     Shelley
2             The Last Man  1826    Mary     Shelley
3  The Old Man and the Sea  1951  Ernest  Hemmingway
4       The Sun Also Rises  1927  Ernest  Hemmingway
pandas के साथ डेटा को रीशेप करना

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

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

Preparing Video For Download...