熊猫 - 列嵌套无值,转换为空字符串

发布于 2025-02-11 03:46:35 字数 1516 浏览 0 评论 0原文

我有以下我转换为DF的示例列表:

new_list = [{'video_id': {'platform': 'facebook', 'id':'123'}, 'title': 'Scam Rapper', 'description': 'truthful', 'keywords':['x', 'B'], 'publish_date': '2022-06-15', 'publish_timestamp':'2022-06-15 23:30:02', 'publisher': {'creator_id': '2pal', 'creator_name': 'X'}, 'thumbnail_url': 'https://sc4', 'video_url':'https:/', 'duration': 278, 'views': 836977, 'engagements': {'total':18463, 'breakdown': [{'platform': 'facebook', 'total': 18463, 'likes':9436, 'shares': 5581, 'comments': 608, 'tweets': None, 'favorites':None, 'hahas': 1049, 'wows': 170, 'loves': 1554, 'sads': 44, 'angrys':21}]}, 'categories': [{'category_name': 'News, Government & Politics'}, {'category_name': 'News, Government & Politics/News'},{'category_name': 'News, Government & Politics/News/Crime'}],'language': 'en', 'tvr': {'v1': 456443, 'v2': 673473, 'v3': 739055,'v7': 832210, 'v30': None, 'er1': 0.638300001621246, 'er2':0.689400017261505, 'er3': 0.7175999879837031, 'er7': 0.7592999935150141, 'er30': None}, 'video_was_live': False}]

video_items_df = pd.DataFrame(new_list)

video_items_df = pd.concat([video_items_df.drop(['engagements'], axis=1), video_items_df['engagements'].apply(pd.Series)], axis=1)

video_items_df = pd.concat(
            [video_items_df.drop(['video_id'], axis=1), video_items_df['video_id'].apply(pd.Series)], axis=1)

但是您会在现在创建的“分解”列中看到,没有值,我尝试通过添加它来将其转换为字符串,但我认为它不起作用因为它没有嵌套

video_items_df.fillna("",inplace=True)

I have the following example list that I convert to a df:

new_list = [{'video_id': {'platform': 'facebook', 'id':'123'}, 'title': 'Scam Rapper', 'description': 'truthful', 'keywords':['x', 'B'], 'publish_date': '2022-06-15', 'publish_timestamp':'2022-06-15 23:30:02', 'publisher': {'creator_id': '2pal', 'creator_name': 'X'}, 'thumbnail_url': 'https://sc4', 'video_url':'https:/', 'duration': 278, 'views': 836977, 'engagements': {'total':18463, 'breakdown': [{'platform': 'facebook', 'total': 18463, 'likes':9436, 'shares': 5581, 'comments': 608, 'tweets': None, 'favorites':None, 'hahas': 1049, 'wows': 170, 'loves': 1554, 'sads': 44, 'angrys':21}]}, 'categories': [{'category_name': 'News, Government & Politics'}, {'category_name': 'News, Government & Politics/News'},{'category_name': 'News, Government & Politics/News/Crime'}],'language': 'en', 'tvr': {'v1': 456443, 'v2': 673473, 'v3': 739055,'v7': 832210, 'v30': None, 'er1': 0.638300001621246, 'er2':0.689400017261505, 'er3': 0.7175999879837031, 'er7': 0.7592999935150141, 'er30': None}, 'video_was_live': False}]

video_items_df = pd.DataFrame(new_list)

video_items_df = pd.concat([video_items_df.drop(['engagements'], axis=1), video_items_df['engagements'].apply(pd.Series)], axis=1)

video_items_df = pd.concat(
            [video_items_df.drop(['video_id'], axis=1), video_items_df['video_id'].apply(pd.Series)], axis=1)

But you'll see that in the breakdown column now created, there is a None value, I tried to convert it to string by adding this but it didn't work I think because its a nested None

video_items_df.fillna("",inplace=True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所的.畏惧 2025-02-18 03:46:35

使用 > with dict coldection pracsions

In [1664]: video_items_df['breakdown'] = video_items_df.breakdown.apply(lambda x: {k: v or '' for (k,v) in x[0].items()})

In [1665]: video_items_df.breakdown[0]
Out[1665]: 
{'platform': 'facebook',
 'total': 18463,
 'likes': 9436,
 'shares': 5581,
 'comments': 608,
 'tweets': '',
 'favorites': '',
 'hahas': 1049,
 'wows': 170,
 'loves': 1554,
 'sads': 44,
 'angrys': 21}

Use df.apply with dict comprehension:

In [1664]: video_items_df['breakdown'] = video_items_df.breakdown.apply(lambda x: {k: v or '' for (k,v) in x[0].items()})

In [1665]: video_items_df.breakdown[0]
Out[1665]: 
{'platform': 'facebook',
 'total': 18463,
 'likes': 9436,
 'shares': 5581,
 'comments': 608,
 'tweets': '',
 'favorites': '',
 'hahas': 1049,
 'wows': 170,
 'loves': 1554,
 'sads': 44,
 'angrys': 21}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文