[英]ValueError: Unknown format code 'f' for object of type 'str'
[英]ValueError: Unknown format code 'f' for object of type 'str' - why do I get this the second time but not the first time?
下面是我的代碼。 我正在嘗試同時打印前250行和后250行,並制定策略以制作我的主數據框的副本,重新排序,然后將百分比格式設置為帶有“%”符號的字符串格式。 不幸的是,在upreportdataframe而不是downreportdataframe的行上,我收到ValueError:類型為'str'的對象的未知格式代碼'f'。 為什么會這樣呢?
這與數據幀的復制方式有關嗎?
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
不是副本upreportdataframe
; 相反,它只是對同一對象的另一個引用。
如果要復制,請使用dataframe.copy()
方法 :
downreportdataframe = upreportdataframe.copy()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.