熊猫 - 列嵌套无值,转换为空字符串
我有以下我转换为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 > with
dict coldection
pracsions :Use
df.apply
withdict comprehension
: