【问题标题】:How to write this python code. I have tried it but didn't work如何编写此 Python 代码。我试过但没有用
【发布时间】:2020-07-03 17:09:14
【问题描述】:
from seaborn import load_dataset

df = load_dataset("diamonds")

df.head()

例如,我想创建一个钻石列表,其中每个元素都是一个字典,键是克拉、切工、颜色、净度、深度、表和价格,值是它们在 df_new 中的对应值。钻石中的第一个元素如下所示谢谢

{'carat': 0.23,

'cut': 'Ideal',

'color': 'E',

'clarity': 'SI2',

'depth': 61.5,

'table': 55.0,

'price': 326}

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    要创建具有特定字段的字典列表,请使用

    df[['carat', 'cut', 'color', 'clarity', 'depth', 'table', 'price']].to_dict('records')
    

    这是第一个条目:

    {'carat': 0.23,
     'cut': 'Ideal',
     'color': 'E',
     'clarity': 'SI2',
     'depth': 61.5,
     'table': 55.0,
     'price': 326}
    

    【讨论】:

      【解决方案2】:

      创建您的字典/列表:

      diamonds = {
      "carat": 0.23,
      "cut": "Ideal",
      "color": "E"
      }
      

      验证是否正确:

      print(diamonds)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多