2017-10-16 35 views
1

The documentation似乎有點稀疏,爲每一個元素是如何工作的,所以這裏有雲:遍歷分組大熊貓DF和出口個別地塊

我有一堆文件,我想遍歷並輸出一個陰謀,爲每一個單一的文件。

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: tuple indices must be integers or slices, not str。我爲什麼得到這個?

回答

1

我認爲你需要改變:

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") 

爲解壓tuples

或選擇[1]元組的第二個值:

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 ) 

VS:

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

您可以通過檢查tuple輸出

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])) 
+0

現在我得到'類型錯誤:無法轉換24540.50005151.2500633.5000550.0000-1204.50002094.0000-1045.2500742.750052.75003610.75009719(...),以numeric.' 現在看來似乎有一些問題解析數據幀,儘管打印看起來非常好。是什麼賦予了? –

+1

我想你需要'df ['Dem-Dexc'] = df ['Dem-Dexc']。astype(float)',因爲在列中是字符串。 – jezrael

+0

你說得對。這是我的錯誤。似乎我的一些數據框在初始導入時被組合爲字符串。謝謝! –