且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python Pandas中拆分和连接数据框以使用rpy2进行绘图

更新时间:2022-06-19 01:37:01

当然,您可以使用以下方法来简化操作:

Certainly you can simplify by using:

df1['label'] = 'df1'

(而不是df1["label"] = len(df1.index) * ["df1"].)

如果您发现自己经常这样做,为什么不创建自己的函数呢? (类似这样):

If you find yourself doing this a lot, why not create your own function? (something like this):

plot_dfs(dfs):
    for i, df in enumerate(dfs):
        df['label'] =  'df%s' % i+1 # note: this *changes* df
    melted_df = pd.concat(dfs)

    # plot parameters from melted_df and colour them by df1 or df2
    ggplot2.ggplot(melted_df) + ggplot2.ggplot(aes_string(..., colour="label"))

    return # the melted_df or ggplot ?