2016-12-23 11 views
2

私はPandasのチュートリアルを行っていました。私は真っ直ぐに進むべきだと思ったものを途中で試してみることにしました。個人的に再現するための単純なコードに凝縮し、Pythonでのエラーやエラーの表示を助けます。Forループを使用してPandasのDataFrames辞書を変更する

df = pd.DataFrame({'A': 1., 
        'B': pd.Timestamp('20130102'), 
        'C': pd.Series(1, index = list(range(4)), dtype = 'float32'), 
        'D': np.array([3] * 4, dtype = 'int32'), 
        'E': pd.Categorical(["test", "train", "test", "train"]), 
        'F': 'foo' 
        }) 

# Made copy of df and modified it individually to show that it works. 
df2 = df 
df2.drop([1,3], inplace=True) # Dropping 2nd and 5th row. 
print(df2) 

# Now trying to do the same for multiple dataframes in a 
# dictionary keeps giving me an error. 

dic = {'1900' : df, '1901' : df, '1902' : df} # Dic w/ 3 pairs. 
names = ['1900', '1901', '1902']    # The dic keys in list. 

# For loop to drop the 2nd and 4th row. 
for ii in names: 
    df_dic = dic[str(ii)] 
    df_dic.drop([1,3], inplace=True) 
    dic[str(ii)] = df_dic 

私が手出力は次のようになります。

 A   B C D  E F 
0 1.0 2013-01-02 1.0 3 test foo 
2 1.0 2013-01-02 1.0 3 test foo 
-------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-139-8236a9c3389e> in <module>() 
    21 for ii in names: 
    22  df_dic = dic[str(ii)] 
---> 23  df_dic.drop([1,3], inplace=True) 

C:\Anaconda3\lib\site-packages\pandas\core\generic.py in drop(self, labels, axis, level, inplace, errors) 
    1905     new_axis = axis.drop(labels, level=level, errors=errors) 
    1906    else: 
-> 1907     new_axis = axis.drop(labels, errors=errors) 
    1908    dropped = self.reindex(**{axis_name: new_axis}) 
    1909    try: 

C:\Anaconda3\lib\site-packages\pandas\indexes\base.py in drop(self, labels, errors) 
    3260    if errors != 'ignore': 
    3261     raise ValueError('labels %s not contained in axis' % 
-> 3262         labels[mask]) 
    3263    indexer = indexer[~mask] 
    3264   return self.delete(indexer) 

ValueError: labels [1 3] not contained in axis 

だから、明らかにそれは私に所望の出力を与えたので、それは個別に働く行っているときの行をドロップします。 For Loopで実装するのはなぜですか?

ありがとうございます。

+1

あなたは 'df_dic = dic [str(ii)]のように' copy'を追加する必要があると思います。コピー() ' – jezrael

答えて

2

あなたはcopyDataFrame必要があります。

for ii in names: 
    df_dic = dic[str(ii)].copy() 
    df_dic.drop([1,3], inplace=True) 
    dic[str(ii)] = df_dic 

print (dic) 
{'1900':  A   B C D  E F 
0 1.0 2013-01-02 1.0 3 test foo 
2 1.0 2013-01-02 1.0 3 test foo, '1902':  A   B C D  E F 
0 1.0 2013-01-02 1.0 3 test foo 
2 1.0 2013-01-02 1.0 3 test foo, '1901':  A   B C D  E F 
0 1.0 2013-01-02 1.0 3 test foo 
2 1.0 2013-01-02 1.0 3 test foo} 

Copying in docsを。

+0

それはかなり意味があります。しかし、 'copy()'を追加しても、まったく同じエラーが出ます。また、エラーメッセージの 'df_dic = dic [str(ii)]。copy()'の後の行を指しています。私はあなたが非常に迅速に返答したことを感謝します。 – ssurendr

+2

あなたのコードの 'df2 = df'が重要なのは' copy'です。 ;) – jezrael

+0

ハイファイブ!それはそれだった。どうもありがとうございました!ところで、私はあなたに何かへのリンクがあると誓っていたかもしれません。それは今なくなってしまった。私を助けてくれるものでしたか? – ssurendr

関連する問題