在数据框架内解包字典

2024-06-17 13:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含一系列字典的熊猫数据框,如下所示:

df.head()

Index                 params                    score            
0   {'n_neighbors': 1, 'weights': 'uniform'}    0.550
1   {'n_neighbors': 1, 'weights': 'distance'}   0.550
2   {'n_neighbors': 2, 'weights': 'uniform'}    0.575
3   {'n_neighbors': 2, 'weights': 'distance'}   0.550
4   {'n_neighbors': 3, 'weights': 'uniform'}    0.575

其目的是为每个实例创建一个数据帧,其中“n\u neighbors”和“weights”作为属性,并删除params列。我通过创建空numpy数组、循环和附加来实现这一点:

n_neighbors = np.array([])
weights = np.array([])

count = sum(df["score"].value_counts())

for x in range(count):
     n_neighbors = np.append(n_neighbors, df["params"][x]["n_neighbors"])

for x in range(count):
     weights = np.append(weights, df["params"][x]["weights"])

df["n_neighbors"] = n_neighbors
df["weights"] = weights
df = df.drop(["params"], axis=1)

这感觉又脏又没效率。有没有更优雅的方法来实现这一点?你知道吗


Tags: 数据indfforcountnpneighborsrange
3条回答

在你的情况下,你不需要numpy。普通的python列表感觉更好。我提醒你,df实际上是一个字典列表(每一行都是一个类似的dict)。检查Doku ex:d={'col1':[1,2],'col2':[3,4]}。所以要遵循这个模式。 当你把它传给构造器的时候pd.数据帧()

我想这是正确的方法。你知道吗

简单

datapoints = list(dataframe['params'])
data = pd.DataFrame(datapoints)
data['score'] = list(dataframe['score'])

df['params']构造一个新的数据帧,并将其连接到原始数据帧。为了方便起见,^{}同时返回一个序列并将其从数据帧中删除。你知道吗

df = pd.DataFrame({'Index': [0, 1],
                   'params': [{'n_neighbors': 1, 'weights': 'uniform'},
                              {'n_neighbors': 1, 'weights': 'distance'}],
                   'score': [0.550, 0.550]})

res = df.join(pd.DataFrame(df.pop('params').tolist()))

print(res)

   Index  score  n_neighbors   weights
0      0   0.55            1   uniform
1      1   0.55            1  distance

相关问题 更多 >