gpt4 book ai didi

python - 如何将特定dtype对象列的字段转换为pandas中数据框的列

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:56 25 4
gpt4 key购买 nike

我正在尝试将对象类型列 page_view_count 字段转换为数据框的列。

我有一个数据框:

    _id                         page_view_count
568a8c25cac4991645c287ac {u'main-rating': 2, u'detailed-rating2': 1, u'detailed-rating': 2}
568cd22e9e82dfc166d7dff1 {u'main-rating': 1, u'thank-you': 1, u'detailed-rating2': 1, u'detailed-rating': 1, u'comments': 1}
568e5a38b4a797c664143dda {u'main-rating': 1, u'detailed-rating2': 1, u'detailed-rating': 1}
568e5a561ae56e09656bfb99 {u'main-rating': 1, u'detailed-rating': 1}
56b24c651fd6901e0ac262e4 nan
568df45a177e30c6487d3600 {u'main-rating': 1, u'thank-you': 1, u'detailed-rating2': 1, u'detailed-rating': 1, u'comments': 1}

我希望 page_view_count 的字段作为数据框的列:

_id                         main-rating     detailed-rating     detailed-rating2    comments    thank-you
568a8c25cac4991645c287ac 2 1 1 nan nan
568cd22e9e82dfc166d7dff1 1 1 1 1 1
568e5a38b4a797c664143dda 1 1 1 nan nan
568e5a561ae56e09656bfb99 1 1 nan nan nan
56b24c651fd6901e0ac262e4 nan nan nan nan nan
568df45a177e30c6487d3600 1 1 1 1 1

有什么办法吗?

最佳答案

您可以从列 page_view_countjoin 创建新的数据框列_id。最后sort_index :

df1 = pd.DataFrame([x for x in df['page_view_count']]).join(df['_id'])
df1 = df1.sort_index(1)
print df1
_id comments detailed-rating detailed-rating2 \
0 568a8c25cac4991645c287ac NaN 2 1
1 568cd22e9e82dfc166d7dff1 1 1 1
2 568e5a38b4a797c664143dda NaN 1 1
3 568e5a561ae56e09656bfb99 NaN 1 NaN
4 568df45a177e30c6487d3600 1 1 1

main-rating thank-you
0 2 NaN
1 1 1
2 1 NaN
3 1 NaN
4 1 1

编辑:

连接时 NaN 仍然存在问题。解决方案是将 NaN 替换为空字典 fillna然后创建Dataframe:

import pandas as pd
import numpy as np


df = pd.DataFrame([[1, {'name':'Jack','email':'abc'} ],
[2, np.nan],
[3, {'name':'Ram','email':'xyz'} ],
], columns=['_id','page_view_count'])

print df[df['page_view_count'].isnull()].index
#Int64Index([1], dtype='int64')

print pd.Series([{}], index=df[df['page_view_count'].isnull()].index , name='page_view_count')
#1 {}
#Name: page_view_count, dtype: object

df['page_view_count'] = df['page_view_count'].fillna(pd.Series([{}], index=df[df['page_view_count'].isnull()].index , name='page_view_count'))
print df
# _id page_view_count
#0 1 {u'name': u'Jack', u'email': u'abc'}
#1 2 {}
#2 3 {u'name': u'Ram', u'email': u'xyz'}


df1 = pd.DataFrame([x for x in df['page_view_count']]).join(df['_id'], how='right')
df1 = df1.sort_index(1)
print df1
# _id email name
#0 1 abc Jack
#1 2 NaN NaN
#2 3 xyz Ram

关于python - 如何将特定dtype对象列的字段转换为pandas中数据框的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34991666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com