Python 使用For循环修改Pandas中的数据帧字典
我正在看一本关于熊猫的教程。我决定用我认为应该是直截了当的方法做一半的实验。我将它浓缩成一个简单的代码,供其他人亲自复制,并帮助我了解我的错误或Python中的错误Python 使用For循环修改Pandas中的数据帧字典,python,pandas,dictionary,for-loop,dataframe,Python,Pandas,Dictionary,For Loop,Dataframe,我正在看一本关于熊猫的教程。我决定用我认为应该是直截了当的方法做一半的实验。我将它浓缩成一个简单的代码,供其他人亲自复制,并帮助我了解我的错误或Python中的错误 df = pd.DataFrame({'A': 1., 'B': pd.Timestamp('20130102'), 'C': pd.Series(1, index = list(range(4)), dtype = 'float32'),
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
abcdef
0 1.0 2013-01-02 1.0 3测试食品
2 1.0 2013-01-02 1.0 3测试食品
--------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
21关于ii的名称:
22 df_dic=dic[str(ii)]
--->23方向盘下降([1,3],原地=真)
C:\Anaconda3\lib\site packages\pandas\core\generic.py放置(self、labels、axis、level、inplace、errors)
1905新_轴=轴。放置(标签,级别=级别,错误=错误)
1906年其他:
->1907新_轴=轴。下拉(标签,错误=错误)
1908 drop=self.reindex(**{axis\u name:new\u axis})
1909尝试:
C:\Anaconda3\lib\site packages\pandas\index\base.py放入(self、labels、errors)
3260如果出现错误!='忽略':
3261 raise VALUERROR('轴中不包含标签%s'%
->3262标签[遮罩])
3263索引器=索引器[~掩码]
3264返回自删除(索引器)
ValueError:轴中不包含标签[1 3]
所以很明显,在单独执行时删除行是有效的,因为它给了我所需的输出。为什么在For循环中实现会使其行为异常
提前感谢。您需要数据帧
:
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}
.我认为你需要添加copy
像df_dic=dic[str(ii)]。copy()
这很有意义。但是在添加了copy()
之后,我仍然得到了完全相同的错误。它指向错误消息中df_dic=dic[str(ii)].copy()之后的行。我很感激你回复得很快。也许在你的代码中添加df2=df
也很重要击掌!!就这样。非常感谢你!顺便说一下,我可以发誓你和什么东西有联系。它现在不见了。这对我有帮助吗?我想没有,我只是在答案的末尾加了一个链接。是的,这就是我指的链接。我想我疲惫的眼睛第二次看不见了。哈哈。没问题。您答对了,所以这是一个给定的答案。:)