如何在数据框列(熊猫)中放置特定的字典值

时间:2019-07-01 20:37:14

标签: python pandas dictionary

所以我有一个字典和一个带有已确定列的数据框。例如,我想将字典的每个第一个值放在第二列,并将每个第二个值放在最后一列。因此,我希望能够专门设置值。 这是我的字典和已经制作好的DataFrame:

products = {"Piano": [500, "10/10"],
            "Table": [120, "9/10"],
            "Book": [7, "2/10"]}
df = pd.DataFrame(columns=["Product Name", "Price", "Made in", "Product quality"])

我的图像类似:

df.column[1] = products["Piano"][0]

结果是:

       Product Name  Price  Made in  Product quality
Piano           --    500       --             10/10
Table           --      9       --              9/10
Book            --      2       --              2/10

1 个答案:

答案 0 :(得分:0)

pd.DataFrame.from_dictreindex

df = pd.DataFrame.from_dict(products, 'index', columns=['Price', 'Quality'])
df.reindex(columns=['Name', 'Price', 'MadeIn', 'Quality'])

       Name  Price  MadeIn Quality
Piano   NaN    500     NaN   10/10
Table   NaN    120     NaN    9/10
Book    NaN      7     NaN    2/10