dict轉(zhuǎn)dataframe pandas dataframe通過numpy轉(zhuǎn)為字典?
pandas dataframe通過numpy轉(zhuǎn)為字典?兩種方法:Da=數(shù)據(jù)框至dict(orient=“records”)數(shù)據(jù)框至使用panda進行數(shù)據(jù)統(tǒng)計分析時,可能不知道如何保存groupby函
pandas dataframe通過numpy轉(zhuǎn)為字典?
兩種方法:
Da=數(shù)據(jù)框至dict(orient=“records”)數(shù)據(jù)框至使用panda進行數(shù)據(jù)統(tǒng)計分析時,可能不知道如何保存groupby函數(shù)的分組結(jié)果。我的解決方案是:通過reset index()函數(shù)可以將groupby()的分組結(jié)果轉(zhuǎn)換成dataframe對象,這樣就可以保存了
代碼示例:out xlsx=in f name“-組.xlsx“數(shù)據(jù)框組=數(shù)據(jù)框groupby([“促銷計劃”、“促銷組”]。描述()。重置索引()組.toexcel(outuxlsx,sheetuname=“Sheet1”,index=false)
dataframegroupby怎么變?yōu)閐ataframe?
在數(shù)據(jù)框中,根據(jù)一定的條件,我們可以得到滿足要求的行元素的位置。
代碼如下:
[Python]查看純拷貝
DF=pd.數(shù)據(jù)幀({“BoolCol”:[1,2,3,3,4],“attr”:[22,33,22,44,66]},
index=[10,20,30,40,50])打?。╠f)
a=df[(df.BoolCol==3)&安培(數(shù)據(jù)框?qū)傩?=22)]. 索引.tolist()
打印(a)
DF如下所示。上面,通過選擇“boolcol”值為3,“attr”值為22的行,我們可以得到該行在DF中的位置
注意:返回的位置是索引列表,根據(jù)索引的不同而不同。這很容易成為數(shù)組中的默認(rèn)下標(biāo)。
[python]查看純拷貝
BoolCol attr
10 1 22
20 2 33
30 3 22
40 3 44
50 4 66
[30]