python - 循环分组 pandas df 并导出单个图

标签 python python-3.x pandas pandas-groupby

The documentation至于每个元素如何工作,似乎有点稀疏,所以这里是:

我有一堆文件,我想对每个文件进行迭代并导出绘图。

df_all.head()

返回

    Dem-Dexc    Aem-Dexc    Aem-Aexc    S       E     fit     frame filename
0   18150.0595  18548.2451  15263.7451  0.7063  0.5054  0.879   1.0 Traces_exp22_tif_pair16.txt
1   596.9286    7161.7353   1652.8922   0.8244  0.9231  0.879   2.0 Traces_exp22_tif_pair16.txt
2   93.2976     3112.3725   2632.6667   0.5491  0.9709  0.879   3.0 Traces_exp22_tif_pair16.txt
3   1481.1310   4365.4902   769.3333    0.8837  0.7467  0.879   4.0 Traces_exp22_tif_pair16.txt
4   583.1786    6192.6373   1225.5392   0.8468  0.9139  0.879   5.0 Traces_exp22_tif_pair16.txt

现在我想分组和迭代:

for group in df_all.groupby("filename"):
    plot = sns.regplot(data = group, x = "Dem-Dexc", y = "frame")

但是我得到TypeError:元组索引必须是整数或切片,而不是str。为什么我会得到这个?

最佳答案

我认为你需要改变:

for group in df_all.groupby("filename")

至:

for i, group in df_all.groupby("filename"):
    plot = sns.regplot(data = group, x = "Dem-Dexc", y = "frame")

用于解压元组

或者通过[1]选择元组的第二个值:

for group in df_all.groupby("filename"):
    plot = sns.regplot(data = group[1], x = "Dem-Dexc", y = "frame")

您可以通过以下方式检查tuple输出:

for group in df_all.groupby("filename"):
    print (group)

('Traces_exp22_tif_pair16.txt',      Dem-Dexc    Aem-Dexc    Aem-Aexc       S       E    fit  frame  \
0  18150.0595  18548.2451  15263.7451  0.7063  0.5054  0.879    1.0   
1    596.9286   7161.7353   1652.8922  0.8244  0.9231  0.879    2.0   
2     93.2976   3112.3725   2632.6667  0.5491  0.9709  0.879    3.0   
3   1481.1310   4365.4902    769.3333  0.8837  0.7467  0.879    4.0   
4    583.1786   6192.6373   1225.5392  0.8468  0.9139  0.879    5.0   

                      filename  
0  Traces_exp22_tif_pair16.txt  
1  Traces_exp22_tif_pair16.txt  
2  Traces_exp22_tif_pair16.txt  
3  Traces_exp22_tif_pair16.txt  
4  Traces_exp22_tif_pair16.txt  )

对比:

for i, group in df_all.groupby("filename"):
    print (group)

     Dem-Dexc    Aem-Dexc    Aem-Aexc       S       E    fit  frame  \
0  18150.0595  18548.2451  15263.7451  0.7063  0.5054  0.879    1.0   
1    596.9286   7161.7353   1652.8922  0.8244  0.9231  0.879    2.0   
2     93.2976   3112.3725   2632.6667  0.5491  0.9709  0.879    3.0   
3   1481.1310   4365.4902    769.3333  0.8837  0.7467  0.879    4.0   
4    583.1786   6192.6373   1225.5392  0.8468  0.9139  0.879    5.0   

                      filename  
0  Traces_exp22_tif_pair16.txt  
1  Traces_exp22_tif_pair16.txt  
2  Traces_exp22_tif_pair16.txt  
3  Traces_exp22_tif_pair16.txt  
4  Traces_exp22_tif_pair16.txt  

如果想将输出保存到图片png:

for i, group in df_all.groupby("filename"):
    plot = sns.regplot(data = group, x = "Dem-Dexc", y = "frame")
    fig = plot.get_figure()
    fig.savefig("{}.png".format(i.split('.')[0]))

关于python - 循环分组 pandas df 并导出单个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46767724/

相关文章:

python - 使用 PySpark 展平嵌套 json 响应结构的最有效方法是什么?

python - 重新采样 MultiIndexed Pandas DataFrame 并将不同的函数应用于列

python - 在另一个函数中使用返回值

Python any() 函数对于负数列表没有按预期运行

python - 将具有列表作为值的 Python 字典转换为简单字典

Python:创建幂等初始化器

python - 为什么我的二维列表最多包含 2 个项目

python - 根据条件插入索引新行

python - 将数据帧与时间戳和间隔合并

python - 如何将 timedelta 转换为 Pandas 中的时间?