ValueError:未知格式代码'f'为类型'str'的对象-为什么我第二次得到这个,但不是第一次?

时间:2021-04-09 19:43:33

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. Unfortunately I'm getting a ValueError: Unknown format code 'f' for object of type 'str' on the line with upreportdataframe, but not with downreportdataframe. Why would this be?

下面是我的代码。我正在尝试打印前250行和底部250行,并制定策略来复制我的主dataframe,重新排序,然后将这些百分比格式化成带有“%”符号的字符串格式。不幸的是,我得到了一个ValueError:在与upreportdataframe的行上键入“str”的对象的未知格式代码“f”,但没有使用downreportdataframe。这是为什么呢?

Does this have something to do with how the dataframe is copied?

这是否与dataframe的复制有关?

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)

1 个解决方案

#1


1  

downreportdataframe is not a copy of upreportdataframe; it is instead just another reference to the same object.

downreportdataframe不是upreportdataframe的副本;它只是对同一个对象的另一个引用。

If you wanted a copy, use the dataframe.copy() method:

如果您想要一个副本,请使用dataframe.copy()方法:

downreportdataframe = upreportdataframe.copy()

#1


1  

downreportdataframe is not a copy of upreportdataframe; it is instead just another reference to the same object.

downreportdataframe不是upreportdataframe的副本;它只是对同一个对象的另一个引用。

If you wanted a copy, use the dataframe.copy() method:

如果您想要一个副本,请使用dataframe.copy()方法:

downreportdataframe = upreportdataframe.copy()