Pandas - преобразовать список, представленный в виде строки, в список

У меня есть фрейм данных pandas со столбцом, который по сути является массивом, но представлен в виде строки. Этот фрейм данных может быть воссоздан с помощью

import pandas as pd
df = pd.DataFrame({'user_id': {0: '72965be3-1f0b-4ae5-820d-89edd9b2589f', 1: '1f22d7cb-45d2-47ad-af61-6a492d4cded9'}, 'apps': {0: '[\n  {\n    "name": "Skype",\n    "packageName": "com.skype.raider",\n    "versionCode": 1,\n    "versionName": "2.4.546.18139"\n  },\n  {\n    "name": "Abdul Bari Cartoon HD Videos",\n    "packageName": "com.apricot.abdulbaricartoon",\n    "versionCode": 5,\n    "versionName": "1.5"\n  },\n  {\n    "name": "Khata Book",\n    "packageName": "com.vaibhavkalpe.android.khatabook",\n    "versionCode": 258,\n    "versionName": "3.3.3"\n  },\n  {\n    "name": "RK Links",\n    "packageName": "appinventor.ai_dcesiliguri.pbmpay",\n    "versionCode": 1,\n    "versionName": "1.0"\n  },\n  {\n    "name": "Funny Food 3",\n    "packageName": "biz.arrowstar.funnyfood3",\n    "versionCode": 2000045,\n    "versionName": "2.0.0.45"\n  },\n  {\n    "name": "YONO SBI",\n    "packageName": "com.sbi.lotusintouch",\n    "versionCode": 12213,\n    "versionName": "1.22.13"\n  },\n  {\n    "name": "Where is my Train",\n    "packageName": "com.whereismytrain.android",\n    "versionCode": 612,\n    "versionName": "6.1.2"\n  },\n  {\n    "name": "PhonePe",\n    "packageName": "com.phonepe.app",\n    "versionCode": 303585,\n    "versionName": "3.3.40"\n  },\n  {\n    "name": "Coloring and Learn",\n    "packageName": "com.orange.coloring.learn.kids",\n    "versionCode": 93,\n    "versionName": "1.93"\n  },\n  {\n    "name": "WhatsApp",\n    "packageName": "com.whatsapp",\n    "versionCode": 452973,\n    "versionName": "2.19.258"\n  },\n  {\n    "name": "Cricbuzz",\n    "packageName": "com.cricbuzz.android.vernacular",\n    "versionCode": 31,\n    "versionName": "3.1"\n  },\n  {\n    "name": "PowerDirector",\n    "packageName": "com.cyberlink.powerdirector.DRA140225_01",\n    "versionCode": 64868,\n    "versionName": "5.0.1"\n  },\n  {\n    "name": "Wynk Music",\n    "packageName": "com.bsbportal.music",\n    "versionCode": 194,\n    "versionName": "2.0.7.7"\n  },\n  {\n    "name": "AOS TV",\n    "packageName": "aos.com.aostv",\n    "versionCode": 2,\n    "versionName": "15.0.0"\n  },\n  {\n    "name": "Excel",\n    "packageName": "com.microsoft.office.excel",\n    "versionCode": 2001841485,\n    "versionName": "16.0.11919.20000"\n  },\n  {\n    "name": "Flipkart",\n    "packageName": "com.flipkart.android",\n    "versionCode": 980900,\n    "versionName": "6.11"\n  },\n  {\n    "name": "Wonderslate",\n    "packageName": "com.wonderslate.wonderpublish",\n    "versionCode": 77,\n    "versionName": "4.2"\n  },\n  {\n    "name": "OneNote",\n    "packageName": "com.microsoft.office.onenote",\n    "versionCode": 1803714125,\n    "versionName": "16.0.11425.20118"\n  },\n  {\n    "name": "Hotstar",\n    "packageName": "in.startv.hotstar",\n    "versionCode": 661,\n    "versionName": "8.7.4"\n  },\n  {\n    "name": "Outlook",\n    "packageName": "com.microsoft.office.outlook",\n    "versionCode": 179,\n    "versionName": "2.1.145"\n  },\n  {\n    "name": "MX Player",\n    "packageName": "com.mxtech.videoplayer.ad",\n    "versionCode": 1310001114,\n    "versionName": "1.10.44"\n  },\n  {\n    "name": "SHAREit",\n    "packageName": "com.lenovo.anyshare.gps",\n    "versionCode": 4050128,\n    "versionName": "5.1.28_ww"\n  },\n  {\n    "name": "HiFit",\n    "packageName": "hifit.workout.butt.fitness.weightloss",\n    "versionCode": 381,\n    "versionName": "3.8.1"\n  },\n  {\n    "name": "Udaan",\n    "packageName": "com.udaan.android",\n    "versionCode": 14110,\n    "versionName": "5.4"\n  },\n  {\n    "name": "Word",\n    "packageName": "com.microsoft.office.word",\n    "versionCode": 2001382463,\n    "versionName": "16.0.7766.4775"\n  },\n  {\n    "name": "Paytm",\n    "packageName": "net.one97.paytm",\n    "versionCode": 700298,\n    "versionName": "8.2.3"\n  },\n  {\n    "name": "com.android.partner.tc",\n    "packageName": "com.truecaller.partner.lenovo",\n    "versionCode": 5,\n    "versionName": "1.4"\n  },\n  {\n    "name": "PowerPoint",\n    "packageName": "com.microsoft.office.powerpoint",\n    "versionCode": 2001382409,\n    "versionName": "16.0.7766.4272"\n  },\n  {\n    "name": "IRCTC Rail Connect",\n    "packageName": "cris.org.in.prs.ima",\n    "versionCode": 88,\n    "versionName": "2.1.51"\n  },\n  {\n    "name": "Banner Maker",\n    "packageName": "com.rollerbannermaker",\n    "versionCode": 10,\n    "versionName": "10.0"\n  },\n  {\n    "name": "Puzzle Game",\n    "packageName": "com.puzzlegamesclassic.puzzlegame",\n    "versionCode": 10,\n    "versionName": "1.3.0"\n  },\n  {\n    "name": "SBI Anywhere Personal",\n    "packageName": "com.sbi.SBIFreedomPlus",\n    "versionCode": 81,\n    "versionName": "5.3.22"\n  },\n  {\n    "name": "Snapdeal",\n    "packageName": "com.snapdeal.main",\n    "versionCode": 500364,\n    "versionName": "6.7.4"\n  },\n  {\n    "name": "FastScanner",\n    "packageName": "com.coolmobilesolution.fastscannerfree",\n    "versionCode": 125,\n    "versionName": "4.0.1"\n  },\n  {\n    "name": "Al-Quran (All Language)",\n    "packageName": "com.sleepydev.al_quranoffline",\n    "versionCode": 1,\n    "versionName": "1.0"\n  },\n  {\n    "name": "KineMaster",\n    "packageName": "com.nexstreaming.app.kinemasterfree",\n    "versionCode": 14060,\n    "versionName": "4.11.13.14060.GP"\n  },\n  {\n    "name": "Islam360",\n    "packageName": "com.islam360",\n    "versionCode": 20000030,\n    "versionName": "3.5.0"\n  },\n  {\n    "name": "MatchIt",\n    "packageName": "com.hegodev.matchit",\n    "versionCode": 13,\n    "versionName": "2.2"\n  },\n  {\n    "name": "Marathi Color Keyboard",\n    "packageName": "com.color.apps.marathikeyboard.marathi.language.app.marathi.typing",\n    "versionCode": 10,\n    "versionName": "1.9"\n  },\n  {\n    "name": "انگریزی اردو ڈکشنری",\n    "packageName": "cuznapps.urduenglish.englishurdu.dictionary",\n    "versionCode": 4,\n    "versionName": "1.3"\n  },\n  {\n    "name": "EBook Reader",\n    "packageName": "books.ebook.pdf.reader",\n    "versionCode": 1700,\n    "versionName": "1.7.0.0"\n  },\n  {\n    "name": "Kite",\n    "packageName": "com.zerodha.kite",\n    "versionCode": 62,\n    "versionName": "2.1.15"\n  },\n  {\n    "name": "Kids Urdu Activity",\n    "packageName": "com.suave.urduactivity_book_free",\n    "versionCode": 39,\n    "versionName": "2.7.8"\n  },\n  {\n    "name": "PDF Reader",\n    "packageName": "pdf.reader",\n    "versionCode": 29,\n    "versionName": "6.1"\n  },\n  {\n    "name": "Stylish Urdu Keyboard",\n    "packageName": "com.technofreeapps.urdukeyboardapp",\n    "versionCode": 4,\n    "versionName": "1.4"\n  },\n  {\n    "name": "Meesho",\n    "packageName": "com.meesho.supply",\n    "versionCode": 303,\n    "versionName": "6.5.4"\n  }\n]', 1: '[\n  {\n    "name": "Khata Book",\n    "packageName": "com.vaibhavkalpe.android.khatabook",\n    "versionCode": 276,\n    "versionName": "3.4.11"\n  },\n  {\n    "name": "Truecaller",\n    "packageName": "com.truecaller",\n    "versionCode": 1029005,\n    "versionName": "10.29.5"\n  },\n  {\n    "name": "SwiftKey Keyboard",\n    "packageName": "com.touchtype.swiftkey",\n    "versionCode": 812123285,\n    "versionName": "6.3.8.73"\n  },\n  {\n    "name": "Citizen Calculator",\n    "packageName": "com.vpn.basiccalculator",\n    "versionCode": 21,\n    "versionName": "1.19"\n  },\n  {\n    "name": "WhatsApp",\n    "packageName": "com.whatsapp",\n    "versionCode": 452767,\n    "versionName": "2.19.115"\n  },\n  {\n    "name": "Amazon Shopping",\n    "packageName": "in.amazon.mShop.android.shopping",\n    "versionCode": 16007410,\n    "versionName": "8.0.1.300"\n  },\n  {\n    "name": "Google Indic Keyboard",\n    "packageName": "com.google.android.apps.inputmethod.hindi",\n    "versionCode": 3210015,\n    "versionName": "3.2.1.127799310-armeabi-v7a"\n  },\n  {\n    "name": "Wallpaper Carousel",\n    "packageName": "com.mfashiongallery.emag",\n    "versionCode": 2016092600,\n    "versionName": "M716092600-G"\n  },\n  {\n    "name": "Google Play services for Instant Apps",\n    "packageName": "com.google.android.instantapps.supervisor",\n    "versionCode": 39948,\n    "versionName": "4.19-release-246143591"\n  },\n  {\n    "name": "MakeMyTrip",\n    "packageName": "com.makemytrip",\n    "versionCode": 121,\n    "versionName": "5.5.1"\n  },\n  {\n    "name": "KLO Bugreport",\n    "packageName": "com.miui.klo.bugreport",\n    "versionCode": 10041,\n    "versionName": "1.0.41"\n  },\n  {\n    "name": "SwiftKey factory settings",\n    "packageName": "com.swiftkey.swiftkeyconfigurator",\n    "versionCode": 268438544,\n    "versionName": "2.0.0.193"\n  },\n  {\n    "name": "Facebook",\n    "packageName": "com.facebook.katana",\n    "versionCode": 23239543,\n    "versionName": "65.0.0.42.81"\n  },\n  {\n    "name": "SwiftKey Preloaded Languages",\n    "packageName": "com.swiftkey.languageprovider",\n    "versionCode": 0\n  },\n  {\n    "name": "WPS Office",\n    "packageName": "cn.wps.moffice_eng",\n    "versionCode": 137,\n    "versionName": "9.2"\n  },\n  {\n    "name": "Amazon Kindle",\n    "packageName": "com.amazon.kindle",\n    "versionCode": 1146618021,\n    "versionName": "4.22.0.165"\n  }\n]'}})

Данные имеют два столбца — user_id и apps. Столбец apps хранится в виде строки.

type(df.apps[0]) # <class 'str'>
df.apps.dtypes # dtype('O')

Однако столбец apps представляет собой список, хранящийся в виде строки. Я хочу преобразовать его в список. Я пытался использовать ast.literal_eval и apply, используя-

df.apps.apply(lambda x: ast.literal_eval(x))

Проблема в том, что это занимает слишком много времени (> 20 минут для 1,5 млн записей), потребляет много памяти и вызывает сбой python.

Есть ли эффективный способ сделать это?

Не могли бы вы поделиться образцом этого списка приложений, это запрос, чтобы проверить его форматирование

Aryan 11.12.2020 12:30

@AryanMishra, я добавил фрагмент, который помогает воссоздать две строки фрейма данных после импорта панд.

Clock Slave 11.12.2020 12:31
apps столбец — это не просто список, хранящийся в виде строки, это json-документ. Каков желаемый результат после того, как вы сделаете это форматирование? Можете ли вы опубликовать, как должна выглядеть первая строка после форматирования?
Matus Dubrava 11.12.2020 13:00
Почему в Python есть оператор "pass"?
Почему в Python есть оператор "pass"?
Оператор pass в Python - это простая концепция, которую могут быстро освоить даже новички без опыта программирования.
Некоторые методы, о которых вы не знали, что они существуют в Python
Некоторые методы, о которых вы не знали, что они существуют в Python
Python - самый известный и самый простой в изучении язык в наши дни. Имея широкий спектр применения в области машинного обучения, Data Science,...
Основы Python Часть I
Основы Python Часть I
Вы когда-нибудь задумывались, почему в программах на Python вы видите приведенный ниже код?
LeetCode - 1579. Удаление максимального числа ребер для сохранения полной проходимости графа
LeetCode - 1579. Удаление максимального числа ребер для сохранения полной проходимости графа
Алиса и Боб имеют неориентированный граф из n узлов и трех типов ребер:
Оптимизация кода с помощью тернарного оператора Python
Оптимизация кода с помощью тернарного оператора Python
И последнее, что мы хотели бы показать вам, прежде чем двигаться дальше, это
Советы по эффективной веб-разработке с помощью Python
Советы по эффективной веб-разработке с помощью Python
Как веб-разработчик, Python может стать мощным инструментом для создания эффективных и масштабируемых веб-приложений.
1
3
70
3
Перейти к ответу Данный вопрос помечен как решенный

Ответы 3

Это решает ваш вопрос?
ОБЩЕЕ ВРЕМЯ: от 1 до 2 секунд

Здесь я использую библиотеку JSON для выполнения вычислений, как она написана на C (НЕ ЗАПУТАЙТЕСЬ С ПОЛНОЙ ФОРМОЙ JSON), поэтому она вычислительно эффективна и быстра.

import json

df['apps'].iloc[0] = json.loads(df['apps'].iloc[0])
df['apps'].iloc[1] = json.loads(df['apps'].iloc[1])

print(type(df['apps'].iloc[0]))
print(type(df['apps'].iloc[1]))

Если у вас есть другие дополнительные вопросы к этому вопросу, не стесняйтесь спрашивать меня!
Удачного кодирования!

Вы можете использовать встроенный парсер json с Series.apply.

df.apps = df.apps.apply(pd.read_json, typ='series')

Я не знаю, быстрее ли это, чем использование стандартного модуля libraray json, как это предлагается в другом ответе, но вы должны сравнить и сравнить.

Ответ принят как подходящий

Хай! Я надеюсь, что этот код поможет вам.

Код 1

import pandas as pd
import json

df = pd.DataFrame({'user_id': {0: '72965be3-1f0b-4ae5-820d-89edd9b2589f', 1: '1f22d7cb-45d2-47ad-af61-6a492d4cded9'}, 'apps': {0: '[\n  {\n    "name": "Skype",\n    "packageName": "com.skype.raider",\n    "versionCode": 1,\n    "versionName": "2.4.546.18139"\n  },\n  {\n    "name": "Abdul Bari Cartoon HD Videos",\n    "packageName": "com.apricot.abdulbaricartoon",\n    "versionCode": 5,\n    "versionName": "1.5"\n  },\n  {\n    "name": "Khata Book",\n    "packageName": "com.vaibhavkalpe.android.khatabook",\n    "versionCode": 258,\n    "versionName": "3.3.3"\n  },\n  {\n    "name": "RK Links",\n    "packageName": "appinventor.ai_dcesiliguri.pbmpay",\n    "versionCode": 1,\n    "versionName": "1.0"\n  },\n  {\n    "name": "Funny Food 3",\n    "packageName": "biz.arrowstar.funnyfood3",\n    "versionCode": 2000045,\n    "versionName": "2.0.0.45"\n  },\n  {\n    "name": "YONO SBI",\n    "packageName": "com.sbi.lotusintouch",\n    "versionCode": 12213,\n    "versionName": "1.22.13"\n  },\n  {\n    "name": "Where is my Train",\n    "packageName": "com.whereismytrain.android",\n    "versionCode": 612,\n    "versionName": "6.1.2"\n  },\n  {\n    "name": "PhonePe",\n    "packageName": "com.phonepe.app",\n    "versionCode": 303585,\n    "versionName": "3.3.40"\n  },\n  {\n    "name": "Coloring and Learn",\n    "packageName": "com.orange.coloring.learn.kids",\n    "versionCode": 93,\n    "versionName": "1.93"\n  },\n  {\n    "name": "WhatsApp",\n    "packageName": "com.whatsapp",\n    "versionCode": 452973,\n    "versionName": "2.19.258"\n  },\n  {\n    "name": "Cricbuzz",\n    "packageName": "com.cricbuzz.android.vernacular",\n    "versionCode": 31,\n    "versionName": "3.1"\n  },\n  {\n    "name": "PowerDirector",\n    "packageName": "com.cyberlink.powerdirector.DRA140225_01",\n    "versionCode": 64868,\n    "versionName": "5.0.1"\n  },\n  {\n    "name": "Wynk Music",\n    "packageName": "com.bsbportal.music",\n    "versionCode": 194,\n    "versionName": "2.0.7.7"\n  },\n  {\n    "name": "AOS TV",\n    "packageName": "aos.com.aostv",\n    "versionCode": 2,\n    "versionName": "15.0.0"\n  },\n  {\n    "name": "Excel",\n    "packageName": "com.microsoft.office.excel",\n    "versionCode": 2001841485,\n    "versionName": "16.0.11919.20000"\n  },\n  {\n    "name": "Flipkart",\n    "packageName": "com.flipkart.android",\n    "versionCode": 980900,\n    "versionName": "6.11"\n  },\n  {\n    "name": "Wonderslate",\n    "packageName": "com.wonderslate.wonderpublish",\n    "versionCode": 77,\n    "versionName": "4.2"\n  },\n  {\n    "name": "OneNote",\n    "packageName": "com.microsoft.office.onenote",\n    "versionCode": 1803714125,\n    "versionName": "16.0.11425.20118"\n  },\n  {\n    "name": "Hotstar",\n    "packageName": "in.startv.hotstar",\n    "versionCode": 661,\n    "versionName": "8.7.4"\n  },\n  {\n    "name": "Outlook",\n    "packageName": "com.microsoft.office.outlook",\n    "versionCode": 179,\n    "versionName": "2.1.145"\n  },\n  {\n    "name": "MX Player",\n    "packageName": "com.mxtech.videoplayer.ad",\n    "versionCode": 1310001114,\n    "versionName": "1.10.44"\n  },\n  {\n    "name": "SHAREit",\n    "packageName": "com.lenovo.anyshare.gps",\n    "versionCode": 4050128,\n    "versionName": "5.1.28_ww"\n  },\n  {\n    "name": "HiFit",\n    "packageName": "hifit.workout.butt.fitness.weightloss",\n    "versionCode": 381,\n    "versionName": "3.8.1"\n  },\n  {\n    "name": "Udaan",\n    "packageName": "com.udaan.android",\n    "versionCode": 14110,\n    "versionName": "5.4"\n  },\n  {\n    "name": "Word",\n    "packageName": "com.microsoft.office.word",\n    "versionCode": 2001382463,\n    "versionName": "16.0.7766.4775"\n  },\n  {\n    "name": "Paytm",\n    "packageName": "net.one97.paytm",\n    "versionCode": 700298,\n    "versionName": "8.2.3"\n  },\n  {\n    "name": "com.android.partner.tc",\n    "packageName": "com.truecaller.partner.lenovo",\n    "versionCode": 5,\n    "versionName": "1.4"\n  },\n  {\n    "name": "PowerPoint",\n    "packageName": "com.microsoft.office.powerpoint",\n    "versionCode": 2001382409,\n    "versionName": "16.0.7766.4272"\n  },\n  {\n    "name": "IRCTC Rail Connect",\n    "packageName": "cris.org.in.prs.ima",\n    "versionCode": 88,\n    "versionName": "2.1.51"\n  },\n  {\n    "name": "Banner Maker",\n    "packageName": "com.rollerbannermaker",\n    "versionCode": 10,\n    "versionName": "10.0"\n  },\n  {\n    "name": "Puzzle Game",\n    "packageName": "com.puzzlegamesclassic.puzzlegame",\n    "versionCode": 10,\n    "versionName": "1.3.0"\n  },\n  {\n    "name": "SBI Anywhere Personal",\n    "packageName": "com.sbi.SBIFreedomPlus",\n    "versionCode": 81,\n    "versionName": "5.3.22"\n  },\n  {\n    "name": "Snapdeal",\n    "packageName": "com.snapdeal.main",\n    "versionCode": 500364,\n    "versionName": "6.7.4"\n  },\n  {\n    "name": "FastScanner",\n    "packageName": "com.coolmobilesolution.fastscannerfree",\n    "versionCode": 125,\n    "versionName": "4.0.1"\n  },\n  {\n    "name": "Al-Quran (All Language)",\n    "packageName": "com.sleepydev.al_quranoffline",\n    "versionCode": 1,\n    "versionName": "1.0"\n  },\n  {\n    "name": "KineMaster",\n    "packageName": "com.nexstreaming.app.kinemasterfree",\n    "versionCode": 14060,\n    "versionName": "4.11.13.14060.GP"\n  },\n  {\n    "name": "Islam360",\n    "packageName": "com.islam360",\n    "versionCode": 20000030,\n    "versionName": "3.5.0"\n  },\n  {\n    "name": "MatchIt",\n    "packageName": "com.hegodev.matchit",\n    "versionCode": 13,\n    "versionName": "2.2"\n  },\n  {\n    "name": "Marathi Color Keyboard",\n    "packageName": "com.color.apps.marathikeyboard.marathi.language.app.marathi.typing",\n    "versionCode": 10,\n    "versionName": "1.9"\n  },\n  {\n    "name": "انگریزی اردو ڈکشنری",\n    "packageName": "cuznapps.urduenglish.englishurdu.dictionary",\n    "versionCode": 4,\n    "versionName": "1.3"\n  },\n  {\n    "name": "EBook Reader",\n    "packageName": "books.ebook.pdf.reader",\n    "versionCode": 1700,\n    "versionName": "1.7.0.0"\n  },\n  {\n    "name": "Kite",\n    "packageName": "com.zerodha.kite",\n    "versionCode": 62,\n    "versionName": "2.1.15"\n  },\n  {\n    "name": "Kids Urdu Activity",\n    "packageName": "com.suave.urduactivity_book_free",\n    "versionCode": 39,\n    "versionName": "2.7.8"\n  },\n  {\n    "name": "PDF Reader",\n    "packageName": "pdf.reader",\n    "versionCode": 29,\n    "versionName": "6.1"\n  },\n  {\n    "name": "Stylish Urdu Keyboard",\n    "packageName": "com.technofreeapps.urdukeyboardapp",\n    "versionCode": 4,\n    "versionName": "1.4"\n  },\n  {\n    "name": "Meesho",\n    "packageName": "com.meesho.supply",\n    "versionCode": 303,\n    "versionName": "6.5.4"\n  }\n]', 1: '[\n  {\n    "name": "Khata Book",\n    "packageName": "com.vaibhavkalpe.android.khatabook",\n    "versionCode": 276,\n    "versionName": "3.4.11"\n  },\n  {\n    "name": "Truecaller",\n    "packageName": "com.truecaller",\n    "versionCode": 1029005,\n    "versionName": "10.29.5"\n  },\n  {\n    "name": "SwiftKey Keyboard",\n    "packageName": "com.touchtype.swiftkey",\n    "versionCode": 812123285,\n    "versionName": "6.3.8.73"\n  },\n  {\n    "name": "Citizen Calculator",\n    "packageName": "com.vpn.basiccalculator",\n    "versionCode": 21,\n    "versionName": "1.19"\n  },\n  {\n    "name": "WhatsApp",\n    "packageName": "com.whatsapp",\n    "versionCode": 452767,\n    "versionName": "2.19.115"\n  },\n  {\n    "name": "Amazon Shopping",\n    "packageName": "in.amazon.mShop.android.shopping",\n    "versionCode": 16007410,\n    "versionName": "8.0.1.300"\n  },\n  {\n    "name": "Google Indic Keyboard",\n    "packageName": "com.google.android.apps.inputmethod.hindi",\n    "versionCode": 3210015,\n    "versionName": "3.2.1.127799310-armeabi-v7a"\n  },\n  {\n    "name": "Wallpaper Carousel",\n    "packageName": "com.mfashiongallery.emag",\n    "versionCode": 2016092600,\n    "versionName": "M716092600-G"\n  },\n  {\n    "name": "Google Play services for Instant Apps",\n    "packageName": "com.google.android.instantapps.supervisor",\n    "versionCode": 39948,\n    "versionName": "4.19-release-246143591"\n  },\n  {\n    "name": "MakeMyTrip",\n    "packageName": "com.makemytrip",\n    "versionCode": 121,\n    "versionName": "5.5.1"\n  },\n  {\n    "name": "KLO Bugreport",\n    "packageName": "com.miui.klo.bugreport",\n    "versionCode": 10041,\n    "versionName": "1.0.41"\n  },\n  {\n    "name": "SwiftKey factory settings",\n    "packageName": "com.swiftkey.swiftkeyconfigurator",\n    "versionCode": 268438544,\n    "versionName": "2.0.0.193"\n  },\n  {\n    "name": "Facebook",\n    "packageName": "com.facebook.katana",\n    "versionCode": 23239543,\n    "versionName": "65.0.0.42.81"\n  },\n  {\n    "name": "SwiftKey Preloaded Languages",\n    "packageName": "com.swiftkey.languageprovider",\n    "versionCode": 0\n  },\n  {\n    "name": "WPS Office",\n    "packageName": "cn.wps.moffice_eng",\n    "versionCode": 137,\n    "versionName": "9.2"\n  },\n  {\n    "name": "Amazon Kindle",\n    "packageName": "com.amazon.kindle",\n    "versionCode": 1146618021,\n    "versionName": "4.22.0.165"\n  }\n]'}})

df.apps = df.apps.apply(lambda x:json.loads(x))
df.apps[0]

Код 2

import pandas as pd
df = pd.DataFrame({'user_id': {0: '72965be3-1f0b-4ae5-820d-89edd9b2589f', 1: '1f22d7cb-45d2-47ad-af61-6a492d4cded9'}, 'apps': {0: '[\n  {\n    "name": "Skype",\n    "packageName": "com.skype.raider",\n    "versionCode": 1,\n    "versionName": "2.4.546.18139"\n  },\n  {\n    "name": "Abdul Bari Cartoon HD Videos",\n    "packageName": "com.apricot.abdulbaricartoon",\n    "versionCode": 5,\n    "versionName": "1.5"\n  },\n  {\n    "name": "Khata Book",\n    "packageName": "com.vaibhavkalpe.android.khatabook",\n    "versionCode": 258,\n    "versionName": "3.3.3"\n  },\n  {\n    "name": "RK Links",\n    "packageName": "appinventor.ai_dcesiliguri.pbmpay",\n    "versionCode": 1,\n    "versionName": "1.0"\n  },\n  {\n    "name": "Funny Food 3",\n    "packageName": "biz.arrowstar.funnyfood3",\n    "versionCode": 2000045,\n    "versionName": "2.0.0.45"\n  },\n  {\n    "name": "YONO SBI",\n    "packageName": "com.sbi.lotusintouch",\n    "versionCode": 12213,\n    "versionName": "1.22.13"\n  },\n  {\n    "name": "Where is my Train",\n    "packageName": "com.whereismytrain.android",\n    "versionCode": 612,\n    "versionName": "6.1.2"\n  },\n  {\n    "name": "PhonePe",\n    "packageName": "com.phonepe.app",\n    "versionCode": 303585,\n    "versionName": "3.3.40"\n  },\n  {\n    "name": "Coloring and Learn",\n    "packageName": "com.orange.coloring.learn.kids",\n    "versionCode": 93,\n    "versionName": "1.93"\n  },\n  {\n    "name": "WhatsApp",\n    "packageName": "com.whatsapp",\n    "versionCode": 452973,\n    "versionName": "2.19.258"\n  },\n  {\n    "name": "Cricbuzz",\n    "packageName": "com.cricbuzz.android.vernacular",\n    "versionCode": 31,\n    "versionName": "3.1"\n  },\n  {\n    "name": "PowerDirector",\n    "packageName": "com.cyberlink.powerdirector.DRA140225_01",\n    "versionCode": 64868,\n    "versionName": "5.0.1"\n  },\n  {\n    "name": "Wynk Music",\n    "packageName": "com.bsbportal.music",\n    "versionCode": 194,\n    "versionName": "2.0.7.7"\n  },\n  {\n    "name": "AOS TV",\n    "packageName": "aos.com.aostv",\n    "versionCode": 2,\n    "versionName": "15.0.0"\n  },\n  {\n    "name": "Excel",\n    "packageName": "com.microsoft.office.excel",\n    "versionCode": 2001841485,\n    "versionName": "16.0.11919.20000"\n  },\n  {\n    "name": "Flipkart",\n    "packageName": "com.flipkart.android",\n    "versionCode": 980900,\n    "versionName": "6.11"\n  },\n  {\n    "name": "Wonderslate",\n    "packageName": "com.wonderslate.wonderpublish",\n    "versionCode": 77,\n    "versionName": "4.2"\n  },\n  {\n    "name": "OneNote",\n    "packageName": "com.microsoft.office.onenote",\n    "versionCode": 1803714125,\n    "versionName": "16.0.11425.20118"\n  },\n  {\n    "name": "Hotstar",\n    "packageName": "in.startv.hotstar",\n    "versionCode": 661,\n    "versionName": "8.7.4"\n  },\n  {\n    "name": "Outlook",\n    "packageName": "com.microsoft.office.outlook",\n    "versionCode": 179,\n    "versionName": "2.1.145"\n  },\n  {\n    "name": "MX Player",\n    "packageName": "com.mxtech.videoplayer.ad",\n    "versionCode": 1310001114,\n    "versionName": "1.10.44"\n  },\n  {\n    "name": "SHAREit",\n    "packageName": "com.lenovo.anyshare.gps",\n    "versionCode": 4050128,\n    "versionName": "5.1.28_ww"\n  },\n  {\n    "name": "HiFit",\n    "packageName": "hifit.workout.butt.fitness.weightloss",\n    "versionCode": 381,\n    "versionName": "3.8.1"\n  },\n  {\n    "name": "Udaan",\n    "packageName": "com.udaan.android",\n    "versionCode": 14110,\n    "versionName": "5.4"\n  },\n  {\n    "name": "Word",\n    "packageName": "com.microsoft.office.word",\n    "versionCode": 2001382463,\n    "versionName": "16.0.7766.4775"\n  },\n  {\n    "name": "Paytm",\n    "packageName": "net.one97.paytm",\n    "versionCode": 700298,\n    "versionName": "8.2.3"\n  },\n  {\n    "name": "com.android.partner.tc",\n    "packageName": "com.truecaller.partner.lenovo",\n    "versionCode": 5,\n    "versionName": "1.4"\n  },\n  {\n    "name": "PowerPoint",\n    "packageName": "com.microsoft.office.powerpoint",\n    "versionCode": 2001382409,\n    "versionName": "16.0.7766.4272"\n  },\n  {\n    "name": "IRCTC Rail Connect",\n    "packageName": "cris.org.in.prs.ima",\n    "versionCode": 88,\n    "versionName": "2.1.51"\n  },\n  {\n    "name": "Banner Maker",\n    "packageName": "com.rollerbannermaker",\n    "versionCode": 10,\n    "versionName": "10.0"\n  },\n  {\n    "name": "Puzzle Game",\n    "packageName": "com.puzzlegamesclassic.puzzlegame",\n    "versionCode": 10,\n    "versionName": "1.3.0"\n  },\n  {\n    "name": "SBI Anywhere Personal",\n    "packageName": "com.sbi.SBIFreedomPlus",\n    "versionCode": 81,\n    "versionName": "5.3.22"\n  },\n  {\n    "name": "Snapdeal",\n    "packageName": "com.snapdeal.main",\n    "versionCode": 500364,\n    "versionName": "6.7.4"\n  },\n  {\n    "name": "FastScanner",\n    "packageName": "com.coolmobilesolution.fastscannerfree",\n    "versionCode": 125,\n    "versionName": "4.0.1"\n  },\n  {\n    "name": "Al-Quran (All Language)",\n    "packageName": "com.sleepydev.al_quranoffline",\n    "versionCode": 1,\n    "versionName": "1.0"\n  },\n  {\n    "name": "KineMaster",\n    "packageName": "com.nexstreaming.app.kinemasterfree",\n    "versionCode": 14060,\n    "versionName": "4.11.13.14060.GP"\n  },\n  {\n    "name": "Islam360",\n    "packageName": "com.islam360",\n    "versionCode": 20000030,\n    "versionName": "3.5.0"\n  },\n  {\n    "name": "MatchIt",\n    "packageName": "com.hegodev.matchit",\n    "versionCode": 13,\n    "versionName": "2.2"\n  },\n  {\n    "name": "Marathi Color Keyboard",\n    "packageName": "com.color.apps.marathikeyboard.marathi.language.app.marathi.typing",\n    "versionCode": 10,\n    "versionName": "1.9"\n  },\n  {\n    "name": "انگریزی اردو ڈکشنری",\n    "packageName": "cuznapps.urduenglish.englishurdu.dictionary",\n    "versionCode": 4,\n    "versionName": "1.3"\n  },\n  {\n    "name": "EBook Reader",\n    "packageName": "books.ebook.pdf.reader",\n    "versionCode": 1700,\n    "versionName": "1.7.0.0"\n  },\n  {\n    "name": "Kite",\n    "packageName": "com.zerodha.kite",\n    "versionCode": 62,\n    "versionName": "2.1.15"\n  },\n  {\n    "name": "Kids Urdu Activity",\n    "packageName": "com.suave.urduactivity_book_free",\n    "versionCode": 39,\n    "versionName": "2.7.8"\n  },\n  {\n    "name": "PDF Reader",\n    "packageName": "pdf.reader",\n    "versionCode": 29,\n    "versionName": "6.1"\n  },\n  {\n    "name": "Stylish Urdu Keyboard",\n    "packageName": "com.technofreeapps.urdukeyboardapp",\n    "versionCode": 4,\n    "versionName": "1.4"\n  },\n  {\n    "name": "Meesho",\n    "packageName": "com.meesho.supply",\n    "versionCode": 303,\n    "versionName": "6.5.4"\n  }\n]', 1: '[\n  {\n    "name": "Khata Book",\n    "packageName": "com.vaibhavkalpe.android.khatabook",\n    "versionCode": 276,\n    "versionName": "3.4.11"\n  },\n  {\n    "name": "Truecaller",\n    "packageName": "com.truecaller",\n    "versionCode": 1029005,\n    "versionName": "10.29.5"\n  },\n  {\n    "name": "SwiftKey Keyboard",\n    "packageName": "com.touchtype.swiftkey",\n    "versionCode": 812123285,\n    "versionName": "6.3.8.73"\n  },\n  {\n    "name": "Citizen Calculator",\n    "packageName": "com.vpn.basiccalculator",\n    "versionCode": 21,\n    "versionName": "1.19"\n  },\n  {\n    "name": "WhatsApp",\n    "packageName": "com.whatsapp",\n    "versionCode": 452767,\n    "versionName": "2.19.115"\n  },\n  {\n    "name": "Amazon Shopping",\n    "packageName": "in.amazon.mShop.android.shopping",\n    "versionCode": 16007410,\n    "versionName": "8.0.1.300"\n  },\n  {\n    "name": "Google Indic Keyboard",\n    "packageName": "com.google.android.apps.inputmethod.hindi",\n    "versionCode": 3210015,\n    "versionName": "3.2.1.127799310-armeabi-v7a"\n  },\n  {\n    "name": "Wallpaper Carousel",\n    "packageName": "com.mfashiongallery.emag",\n    "versionCode": 2016092600,\n    "versionName": "M716092600-G"\n  },\n  {\n    "name": "Google Play services for Instant Apps",\n    "packageName": "com.google.android.instantapps.supervisor",\n    "versionCode": 39948,\n    "versionName": "4.19-release-246143591"\n  },\n  {\n    "name": "MakeMyTrip",\n    "packageName": "com.makemytrip",\n    "versionCode": 121,\n    "versionName": "5.5.1"\n  },\n  {\n    "name": "KLO Bugreport",\n    "packageName": "com.miui.klo.bugreport",\n    "versionCode": 10041,\n    "versionName": "1.0.41"\n  },\n  {\n    "name": "SwiftKey factory settings",\n    "packageName": "com.swiftkey.swiftkeyconfigurator",\n    "versionCode": 268438544,\n    "versionName": "2.0.0.193"\n  },\n  {\n    "name": "Facebook",\n    "packageName": "com.facebook.katana",\n    "versionCode": 23239543,\n    "versionName": "65.0.0.42.81"\n  },\n  {\n    "name": "SwiftKey Preloaded Languages",\n    "packageName": "com.swiftkey.languageprovider",\n    "versionCode": 0\n  },\n  {\n    "name": "WPS Office",\n    "packageName": "cn.wps.moffice_eng",\n    "versionCode": 137,\n    "versionName": "9.2"\n  },\n  {\n    "name": "Amazon Kindle",\n    "packageName": "com.amazon.kindle",\n    "versionCode": 1146618021,\n    "versionName": "4.22.0.165"\n  }\n]'}})
df.apps = df.apps.apply(lambda x:[json.loads(i+"}") for i in str(x.replace("\n","").replace("]","").replace("[","")+"}").split("},")[:-1]])
df.app[0]

Полученные результаты

[{'name': 'Skype',
  'packageName': 'com.skype.raider',
  'versionCode': 1,
  'versionName': '2.4.546.18139'},
 {'name': 'Abdul Bari Cartoon HD Videos',
  'packageName': 'com.apricot.abdulbaricartoon',
  'versionCode': 5,
  'versionName': '1.5'},
 {'name': 'Khata Book',
  'packageName': 'com.vaibhavkalpe.android.khatabook',
  'versionCode': 258,
  'versionName': '3.3.3'},
 {'name': 'RK Links',
  'packageName': 'appinventor.ai_dcesiliguri.pbmpay',
  'versionCode': 1,
  'versionName': '1.0'},
 {'name': 'Funny Food 3',
  'packageName': 'biz.arrowstar.funnyfood3',
  'versionCode': 2000045,
  'versionName': '2.0.0.45'},
 {'name': 'YONO SBI',
  'packageName': 'com.sbi.lotusintouch',
  'versionCode': 12213,
  'versionName': '1.22.13'},
 {'name': 'Where is my Train',
  'packageName': 'com.whereismytrain.android',
  'versionCode': 612,
  'versionName': '6.1.2'},
 {'name': 'PhonePe',
  'packageName': 'com.phonepe.app',
  'versionCode': 303585,
  'versionName': '3.3.40'},
 {'name': 'Coloring and Learn',
  'packageName': 'com.orange.coloring.learn.kids',
  'versionCode': 93,
  'versionName': '1.93'},
 {'name': 'WhatsApp',
  'packageName': 'com.whatsapp',
  'versionCode': 452973,
  'versionName': '2.19.258'},
 {'name': 'Cricbuzz',
  'packageName': 'com.cricbuzz.android.vernacular',
  'versionCode': 31,
  'versionName': '3.1'},
 {'name': 'PowerDirector',
  'packageName': 'com.cyberlink.powerdirector.DRA140225_01',
  'versionCode': 64868,
  'versionName': '5.0.1'},
 {'name': 'Wynk Music',
  'packageName': 'com.bsbportal.music',
  'versionCode': 194,
  'versionName': '2.0.7.7'},
 {'name': 'AOS TV',
  'packageName': 'aos.com.aostv',
  'versionCode': 2,
  'versionName': '15.0.0'},
 {'name': 'Excel',
  'packageName': 'com.microsoft.office.excel',
  'versionCode': 2001841485,
  'versionName': '16.0.11919.20000'},
 {'name': 'Flipkart',
  'packageName': 'com.flipkart.android',
  'versionCode': 980900,
  'versionName': '6.11'},
 {'name': 'Wonderslate',
  'packageName': 'com.wonderslate.wonderpublish',
  'versionCode': 77,
  'versionName': '4.2'},
 {'name': 'OneNote',
  'packageName': 'com.microsoft.office.onenote',
  'versionCode': 1803714125,
  'versionName': '16.0.11425.20118'},
 {'name': 'Hotstar',
  'packageName': 'in.startv.hotstar',
  'versionCode': 661,
  'versionName': '8.7.4'},
 {'name': 'Outlook',
  'packageName': 'com.microsoft.office.outlook',
  'versionCode': 179,
  'versionName': '2.1.145'},
 {'name': 'MX Player',
  'packageName': 'com.mxtech.videoplayer.ad',
  'versionCode': 1310001114,
  'versionName': '1.10.44'},
 {'name': 'SHAREit',
  'packageName': 'com.lenovo.anyshare.gps',
  'versionCode': 4050128,
  'versionName': '5.1.28_ww'},
 {'name': 'HiFit',
  'packageName': 'hifit.workout.butt.fitness.weightloss',
  'versionCode': 381,
  'versionName': '3.8.1'},
 {'name': 'Udaan',
  'packageName': 'com.udaan.android',
  'versionCode': 14110,
  'versionName': '5.4'},
 {'name': 'Word',
  'packageName': 'com.microsoft.office.word',
  'versionCode': 2001382463,
  'versionName': '16.0.7766.4775'},
 {'name': 'Paytm',
  'packageName': 'net.one97.paytm',
  'versionCode': 700298,
  'versionName': '8.2.3'},
 {'name': 'com.android.partner.tc',
  'packageName': 'com.truecaller.partner.lenovo',
  'versionCode': 5,
  'versionName': '1.4'},
 {'name': 'PowerPoint',
  'packageName': 'com.microsoft.office.powerpoint',
  'versionCode': 2001382409,
  'versionName': '16.0.7766.4272'},
 {'name': 'IRCTC Rail Connect',
  'packageName': 'cris.org.in.prs.ima',
  'versionCode': 88,
  'versionName': '2.1.51'},
 {'name': 'Banner Maker',
  'packageName': 'com.rollerbannermaker',
  'versionCode': 10,
  'versionName': '10.0'},
 {'name': 'Puzzle Game',
  'packageName': 'com.puzzlegamesclassic.puzzlegame',
  'versionCode': 10,
  'versionName': '1.3.0'},
 {'name': 'SBI Anywhere Personal',
  'packageName': 'com.sbi.SBIFreedomPlus',
  'versionCode': 81,
  'versionName': '5.3.22'},
 {'name': 'Snapdeal',
  'packageName': 'com.snapdeal.main',
  'versionCode': 500364,
  'versionName': '6.7.4'},
 {'name': 'FastScanner',
  'packageName': 'com.coolmobilesolution.fastscannerfree',
  'versionCode': 125,
  'versionName': '4.0.1'},
 {'name': 'Al-Quran (All Language)',
  'packageName': 'com.sleepydev.al_quranoffline',
  'versionCode': 1,
  'versionName': '1.0'},
 {'name': 'KineMaster',
  'packageName': 'com.nexstreaming.app.kinemasterfree',
  'versionCode': 14060,
  'versionName': '4.11.13.14060.GP'},
 {'name': 'Islam360',
  'packageName': 'com.islam360',
  'versionCode': 20000030,
  'versionName': '3.5.0'},
 {'name': 'MatchIt',
  'packageName': 'com.hegodev.matchit',
  'versionCode': 13,
  'versionName': '2.2'},
 {'name': 'Marathi Color Keyboard',
  'packageName': 'com.color.apps.marathikeyboard.marathi.language.app.marathi.typing',
  'versionCode': 10,
  'versionName': '1.9'},
 {'name': 'انگریزی اردو ڈکشنری',
  'packageName': 'cuznapps.urduenglish.englishurdu.dictionary',
  'versionCode': 4,
  'versionName': '1.3'},
 {'name': 'EBook Reader',
  'packageName': 'books.ebook.pdf.reader',
  'versionCode': 1700,
  'versionName': '1.7.0.0'},
 {'name': 'Kite',
  'packageName': 'com.zerodha.kite',
  'versionCode': 62,
  'versionName': '2.1.15'},
 {'name': 'Kids Urdu Activity',
  'packageName': 'com.suave.urduactivity_book_free',
  'versionCode': 39,
  'versionName': '2.7.8'},
 {'name': 'PDF Reader',
  'packageName': 'pdf.reader',
  'versionCode': 29,
  'versionName': '6.1'},
 {'name': 'Stylish Urdu Keyboard',
  'packageName': 'com.technofreeapps.urdukeyboardapp',
  'versionCode': 4,
  'versionName': '1.4'}]

Вам нужно только df.apps.apply(json.loads).

รยקคгรђשค 11.12.2020 13:12

Другие вопросы по теме