python - using melt function in groupby for large data sets in python
问题描述
I have one data frame with 1782568 distinct groups.
So, when i melt that data by grouping level my kernal got stuck.
So, I am decided to to melt the data by group wise and then i will combine all of them sequentially.
For that I wrote the following function.
def split(df,key):
df2=pd.DataFrame()
for i in range(df[key].drop_duplicates().shape[0]):
grp_key=tuple(df[key].drop_duplicates().iloc[i,:])
df1=df.groupby(key,as_index=False).
get_group(grp_key).reset_index().drop('index',axis=1)
df2=df2.append(df1.groupby(key,as_index=False).
apply(pd.melt,id_vars=key).reset_index()).dropna()
df2=df2.drop(grep('level',df2.columns),axis=1)
return(df2)
here grep is my user defined function, it is working as grep function in R.
In df i would pass data frame and in key i would pass grouping keys in list format.
But the function also took very huge time to complete the process.
Can any one help me to improve the performance.
Thanks in Advance.
解决方案
推荐阅读
- next.js - 如何将 nextjs 应用程序置于维护模式(使用 Vercel)
- javascript - 如何在javascript中获取上一个日期
- vim - 剪贴板中的错误缩进 vim
- c++ - 如何比较 C++ 中向量的元素?
- python - 在 python 中通过 DBSCAN 对空间(纬度、经度)数据进行聚类后,我可以得到聚类质心吗
- excel - 即使调用的宏尚未完成,有没有办法让 VBScript 进一步工作?
- deep-learning - 将 yolov3-spp.weights 转换为 file.tf 模型
- r - 有没有办法将行转换为 R 中的列?
- android - Android应用程序开发问题
- sql - 连接多个表以计算结果