python-3.x - 从字典中的字典中删除键
问题描述
嗨,我从财务数据的 pandas Dataframe 创建了一个字典。在应用一些计算后,我试图从这个数据框中循环提取两个特定值,并将其添加到一个新的数据框中。为此,作为中间步骤,我从 Dataframe(组合)创建了一个新字典,并将其附加到列表中。这样做时,它将索引号列为字典中每个条目内的额外键,即它在字典中创建了字典。我想删除第二个字典键,在本例中为 992,并将该值添加到外部字典键。
note投资组合是一个更大的数据框,我从中提取了这个特定条目
以下是更大的投资组合数据框的负责人:
portfolios.head()
Returns Volatility MMM Weight ABT Weight ... ADBE Weight AMD Weight AAP Weight AES Weight
0 0.376267 0.221238 0.181285 0.048288 ... 0.113503 0.164995 0.004576 0.040676
1 0.323802 0.193708 0.197259 0.001254 ... 0.162608 0.151378 0.059675 0.096442
2 0.398189 0.224429 0.067056 0.169859 ... 0.142113 0.184807 0.031413 0.027089
3 0.225460 0.172322 0.063022 0.100758 ... 0.075529 0.015270 0.134864 0.136784
4 0.325828 0.212882 0.148907 0.193622 ... 0.022118 0.096442 0.082162 0.004809
这是我获取和转换我想要的特定条目的代码:
list2 = []
GVCindex = portfolios[portfolios['Volatility']==portfolios['Volatility'].min()].index.tolist()
dictGVC = portfolios.loc[GVCindex].to_dict()
list2.append(dictGVC)
fin = pd.DataFrame.from_dict(list2)
这是它目前的样子:
>>> list2
[{'Returns': {932: 0.14045532455897075}, 'Volatility': {932: 0.1582942016003597}, 'MMM Weight': {932: 0.18494322390344745}, 'ABT Weight': {932: 0.029906861753963284}, 'ABBV Weight': {932: 0.1837038795739978}, 'ABMD Weight': {932: 0.0062650919515888715}, 'ACN Weight': {932: 0.15793314037253936}, 'ATVI Weight': {932: 0.0644399756626248}, 'ADBE Weight': {932: 0.01200620828052462}, 'AMD Weight': {932: 0.024667578415588958}, 'AAP Weight': {932: 0.149085877589782}, 'AES Weight': {932: 0.18704816249594283}}]
这就是我希望它看起来的样子:
>>> list2
[{'Returns': 0.14045532455897075, 'Volatility': 0.1582942016003597, 'MMM Weight': 0.18494322390344745, 'ABT Weight': 0.029906861753963284}]
这是完成的数据框:
此数据框是从比上例中更大的列表 2 创建的*
>>> fin.head()
Returns Volatility MMM Weight ... AFL Weight A Weight APD Weight
0 {823: 0.4641212481398529} {823: 0.24708090579365835} {823: 0.03427725029749016} ... NaN NaN NaN
1 {788: 0.47643150225144204} {788: 0.2508983879170438} {788: 0.03386593631658274} ... {788: 0.046989535272586806} NaN NaN
2 {32: 0.45814683192368877} {32: 0.25089789807674673} {32: 0.07310079951400653} ... NaN {32: 0.01274115108933681} NaN
3 {216: 0.47680922654636615} {216: 0.2544050792473164} {216: 0.06114500772292474} ... NaN NaN {216: 0.058250849997334514}
4 {853: 0.45735781176585616} {853: 0.23667403702327827} {853: 0.027942223122145397} ... {853: 0.06445688077317947} NaN NaN
[5 rows x 15 columns]
解决方案
我的值以这种方式返回的原因是因为 DataFrame.to_dict 具有如下默认布局:
'dict' (默认) : 像 {column -> {index -> value}} 这样的字典
为了解决这个问题,我将 orient 更改为记录,即我设置:
dictoptimal = portfolios.loc[optimal1I].to_dict(orient='records')
推荐阅读
- javascript - 尝试循环变量
- python - 优化 tf.map_fn 的 GPU 使用率
- typo3 - 列表视图中显示的类别
- google-sheets - 查询以根据多个条件从不同列中检索值
- excel - 我正在尝试自动化一些 Attachmate Extra!用VBA。如何在倒影上按 Page Down?
- python-3.x - 我们可以用 Python 中的另一个逻辑替换 group by 或 for 循环吗
- java - Hybris:如何使用 impex 导入?
- asp.net-mvc - 有没有办法让表格更宽?也许引导程序在某处设置了最大宽度?
- sql - 如何在特定字段SQL中通过具有已知数据顺序的表进行二进制搜索
- elasticsearch - 在 Elasticsearch 中,如何将时区应用于“source”:“doc['date'].value.dayOfWeek”