简体   繁体   English

ValueError:类型为'str'的对象的未知格式代码'f'-为什么第二次却不是第一次?

[英]ValueError: Unknown format code 'f' for object of type 'str' - why do I get this the second time but not the first time?

Below is my code. 下面是我的代码。 I'm trying to print both the Top 250 lines and the Bottom 250 lines, and strategized to make a copy of my main dataframe, re-sort, and then format the percentages into a string format with a "%" sign. 我正在尝试同时打印前250行和后250行,并制定策略以制作我的主数据框的副本,重新排序,然后将百分比格式设置为带有“%”符号的字符串格式。 Unfortunately I'm getting a ValueError: Unknown format code 'f' for object of type 'str' on the line with upreportdataframe, but not with downreportdataframe. 不幸的是,在upreportdataframe而不是downreportdataframe的行上,我收到ValueError:类型为'str'的对象的未知格式代码'f'。 Why would this be? 为什么会这样呢?

Does this have something to do with how the dataframe is copied? 这与数据帧的复制方式有关吗?

upreportdataframe.sort(['dailypctchange'], ascending = False, inplace=True)
downreportdataframe = upreportdataframe
downreportdataframe.is_copy = False
downreportdataframe.sort(['dailypctchange'], ascending = True, inplace = True)
downreportdataframe['dailypctchange'] = pd.Series(
    ["{0:.2f}%".format(val * 100)
     for val in downreportdataframe['dailypctchange']],
    downreportdataframe.index)

upreportdataframe['dailypctchange'] = pd.Series(
    ["{0:.2f}%".format(val * 100)
     for val in upreportdataframe['dailypctchange']],
    upreportdataframe.index)

downreportdataframe is not a copy of upreportdataframe ; downreportdataframe 不是副本upreportdataframe ; it is instead just another reference to the same object . 相反,它只是对同一对象的另一个引用。

If you wanted a copy, use the dataframe.copy() method : 如果要复制,请使用dataframe.copy()方法

downreportdataframe = upreportdataframe.copy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:类型为“str”的 object 的未知格式代码“f” - ValueError: Unknown format code 'f' for object of type 'str' 在代码中格式化不断导致 ValueError: Unknown format code 'f' for object of type 'str'? - Formatting in a code constantly causes the ValueError: Unknown format code 'f' for object of type 'str'? ValueError:类型为“ str”的对象的未知格式代码“ g” - ValueError: Unknown format code 'g' for object of type 'str' ValueError: 类型“str”的对象的格式代码“e”未知 - ValueError: Unknown format code 'e' for object of type 'str' 类型为'str'的对象的未知格式代码'f'-Folium - Unknown format code 'f' for object of type 'str'- Folium Rc4 decrypt ValueError:object 类型为“str”的未知格式代码“x” - Rc4 decrypt ValueError: Unknown format code 'x' for object of type 'str' 类型“ str”的对象的未知格式代码“ b” - Unknown format code 'b' for object of type 'str' “str”类型对象的未知格式代码“g” - Unknown format code 'g' for object of type 'str' 在我的情况下,“str”类型的 object 的“未知格式代码 'f' 是什么意思? - What does "Unknown format code 'f' for object of type 'str' mean in my case? 'unicode'类型对象的未知格式代码'f' - Unknown format code 'f' for object of type 'unicode'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM