[英]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.