2016-02-25 72 views
0

我有以下代码:SettingWithCopyWarning Python的改变列数据类型的数据帧

block_table[[compared_attribute]] = block_table[[compared_attribute]].astype(int) 

我想换一个列的数据类型。代码正在工作,但是我收到了来自Python的警告:SettingWithCopyWarning: 正试图在来自DataFrame的切片副本上设置一个值。 尝试使用的.loc [row_indexer,col_indexer] =值,而不是

查看文档中的告诫:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 自[K] =值[K2]

我看着这个警告,我读它可创建数据帧的副本,而不是仅仅将其覆盖,所以我尝试没有运气以下解决方案......

block_table.loc[[compared_attribute]] = block_table[[compared_attribute]].astype(int) 




block_table.loc[:,compared_attribute] = block_table[[compared_attribute]].astype(int) 
+0

尝试使用单支架而不是双支架,让我们知道你得到什么。 –

+0

谢谢,我试过了: block_table [comparison_attribute] = block_table [comparison_attribute] .astype(int) 但它仍然给我警告... – user1064285

回答

0

它应该是简单:

block_table.loc[:,compared_attribute] = block_table[compared_attribute].astype(int) 

这是假定比较属性是由列另外,切换在loc部分冒号和比较属性。 如果没有数据看起来像什么和比较属性的样子,也很难回答。