我從MongoDB中獲取數據通過pymongo到Python,然後將其轉換成數據幀的大熊貓如何連接pandas中的數據框?
df = pd.DataFrame(list(db.dataset2.find()))
這是數據看起來像在MongoDB中。
"dish" : [
{
"dish_id" : "005" ,
"dish_name" : "Sandwitch",
"dish_price" : 50,
"coupon_applied" : "Yes",
"coupon_type" : "Rs 20 off"
},
{
"dish_id" : "006" ,
"dish_name" : "Chicken Hundi",
"dish_price" : 125,
"coupon_applied" : "No",
"coupon_type" : "Null"
}
],
我想在pandas數據框中將菜屬性分成兩行。這是做這個的代碼。 (有3個碟文件),所以我通過for循環迭代它。
for i in range(0,len(df.dish)):
data_dish = json_normalize(df['dish'][i])
print data_dish
但它給了我下面的輸出..
coupon_applied coupon_type dish_id dish_name dish_price
0 Yes Rs 20 off 001 Chicken Biryani 120
1 No Null 001 Paneer Biryani 100
coupon_applied coupon_type dish_id dish_name dish_price
0 Yes Rs 40 off 002 Mutton Biryani 130
1 No Null 004 Aaloo tikki 95
coupon_applied coupon_type dish_id dish_name dish_price
0 Yes Rs 20 off 005 Sandwitch 50
1 No Null 006 Chicken Hundi 125
而且我想在下面的格式輸出..
coupon_applied coupon_type dish_id dish_name dish_price
0 Yes Rs 20 off 001 Chicken Biryani 120
1 No Null 001 Paneer Biryani 100
2 Yes Rs 40 off 002 Mutton Biryani 130
3 No Null 004 Aaloo tikki 95
4 Yes Rs 20 off 005 Sandwitch 50
5 No Null 006 Chicken Hundi 125
你能幫助我嗎?在此先感謝:)
爲什麼不直接從mangoDB數據創建'pd.DataFrame()'? –
請考慮給我們一個完整的數據框樣本,以便重現問題。 –