如何将此 Pandas 代码优化为更快 运行

How to optimize this Pandas code to run faster

我有这段代码可以根据来自 DataFrame 的数据创建群图:

df = pd.DataFrame({"Refined__Some_ID":some_id_list,
                   "Refined_Age":age_list,
                   "Name":name_list                   
                          }
                         )
#Creating dataframe with strings from the lists
select  = df.apply(lambda row : any([isinstance(e, str) for e in row  ]),axis=1) 
#Exlcluding data from select in a new dataframe
dfAnalysis = df[~select]
dfAnalysis['Refined_Age'].replace('', np.nan, inplace=True)
dfAnalysis = dfAnalysis.dropna()
dfAnalysis['Refined_Age'] = dfAnalysis['Refined_Age'].apply(int)
# print dfAnalysis
print type(dfAnalysis['Refined_Patient_Age'][1])
g = sns.swarmplot(x = dfAnalysis['Refined_ID'],y = dfAnalysis['Refined_Age'], hue = dfAnalysis['Name'], orient="v")
g.set_xticklabels(g.get_xticklabels(),rotation=30)
# print g

运行 花费了大量时间(14 小时,而且还在增加!)。我怎样才能加快速度?另外,为什么代码一开始就这么慢?

数据框中包含的 3 个列表来自 Couchdb 数据库,其中包含大约 320k 个文档。

更新 1

我原本打算只查看前 20 个类别,但排除了这样做的代码。

该行应该是:

x = dfAnalysis['Refined_ID'].iloc[:20]

你说的是几十万个点的swarmplot吗?除了它会永远,这是无稽之谈。尝试使用前 1000 个,看看你会得到什么样的混乱。然后改用箱线图或小提琴图。在使用之前尝试了解您的工具。

来自文档字符串:

[...] it does not scale as well to large numbers of observations (both in terms of the ability to show all the points and in terms of the computation needed to arrange them).