SettingWithCopyWarning Python在Dataframe中更改列数据类型

时间:2016-02-25 18:59:02

标签: python warnings type-conversion

我有以下代码:

 string[] command = Console.ReadLine().Split(' ');
 switch(command[0])
 {
 // Example
 case "createdir":
     if(command.Length > 0)
     {
try
{
       Directory.Create(command.ToString().Replace("createdir ", string.Empty);
}
               // Catch all exceptions.
               catch (IOException ioe)
                {
                    Console.Error.WriteLine("$error->" + ioe.ToString());
                }
                catch (UnauthorizedAccessException uae)
                {
                    Console.Error.WriteLine("$error->" + uae.ToString());
                }
                catch(ArgumentNullException ane)
                {
                    Console.Error.WriteLine("$error->" + ane.ToString());
                }
     }
     else
     {
     try
                {
                    Directory.CreateDirectory(directory_loc);
                    Console.WriteLine("Directory created!");
                }
                // Catch all exceptions
                catch (IOException ioe)
                {
                    Console.Error.WriteLine("$error->" + ioe.ToString());
                }
                catch (UnauthorizedAccessException uae)
                {
                    Console.Error.WriteLine("$error->" + uae.ToString());
                }
                catch(ArgumentNullException ane)
                {
                    Console.Error.WriteLine("$error->" + ane.ToString());
                }
     }
 }

我想更改列的数据类型。代码正常,但我从Python收到警告:SettingWithCopyWarning: 尝试在DataFrame的切片副本上设置值。 尝试使用.loc [row_indexer,col_indexer] = value而不是

请参阅文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy   self [k1] = value [k2]

我查看了这个警告并且我正在阅读它可能正在创建数据框的副本,而不是仅仅覆盖它,所以我尝试了以下解决方案而没有运气......

Console.ReadLine()

1 个答案:

答案 0 :(得分:0)

应该如此简单:

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

这是假设比较属性是按列,否则,切换loc部分中的冒号和comparison_attribute。 如果没有数据的样子和者compare_attribute的样子,也很难回答。

相关问题