dataframe行列互換 dataframegroupby怎么變?yōu)閐ataframe?
dataframegroupby怎么變?yōu)閐ataframe?使用pandas進行數(shù)據(jù)統(tǒng)計分析時,您可能不知道如何保存groupby函數(shù)的分組結(jié)果。我的解決方案是:通過reset index()函數(shù)可以
dataframegroupby怎么變?yōu)閐ataframe?
使用pandas進行數(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)
pandas.dataframe怎么把列變成索引?
在數(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()
打?。╝)
DF如下所示。上面,通過選擇“boolcol”值為3,“attr”值為22的行,我們可以得到該行在DF中的位置
注意:返回的位置是索引列表,根據(jù)索引的不同而不同。這很容易成為數(shù)組中的默認下標。
[Python]查看純拷貝
boolcol attr
10 1 22
20 2 33
30 3 22
40 3 44
50 4 66
[30
兩種方法:
Da=數(shù)據(jù)框至dict(orient=“records”)數(shù)據(jù)框至json(orient=“records”)
在[3]:importpandasaspdIn[4]:a=pd系列[5]中的([1,2,3]):b=pd系列[6]中的([2,3,4]):c= pd.數(shù)據(jù)幀[7]中的([a,b]):cOut[7]:01231234