格式化数据框中的多列 pandas
Formatting multiple columns in dataframe pandas
我有多个列要格式化为具有固定精度的浮点小数。但是,同时处理多个列是行不通的。在各个列上工作。是什么原因,如何解决?
以下作品。
def shortenlength(numberToShorten):
limited_float = "{:.15f}".format(numberToShorten)
return limited_float
outputData['col1'] = outputData['col1'].apply(shortenlength)
outputData['col2'] = outputData['col2'].apply(shortenlength)
但是,以下内容不起作用并引发错误
TypeError: 传递给 Series 的格式字符串不受支持。format
def shortenlength(numberToShorten):
limited_float = "{:.15f}".format(numberToShorten)
return limited_float
zfill_cols = ['col1', 'col2']
outputData[zfill_cols] = outputData[zfill_cols].apply(shortenlength)
当您对数据框执行 apply
时,传递给函数的参数是列,即系列。使用 applymap
代替:
outputData[zfill_cols] = outputData[zfill_cols].applymap(shortenlength)
我有多个列要格式化为具有固定精度的浮点小数。但是,同时处理多个列是行不通的。在各个列上工作。是什么原因,如何解决?
以下作品。
def shortenlength(numberToShorten):
limited_float = "{:.15f}".format(numberToShorten)
return limited_float
outputData['col1'] = outputData['col1'].apply(shortenlength)
outputData['col2'] = outputData['col2'].apply(shortenlength)
但是,以下内容不起作用并引发错误 TypeError: 传递给 Series 的格式字符串不受支持。format
def shortenlength(numberToShorten):
limited_float = "{:.15f}".format(numberToShorten)
return limited_float
zfill_cols = ['col1', 'col2']
outputData[zfill_cols] = outputData[zfill_cols].apply(shortenlength)
当您对数据框执行 apply
时,传递给函数的参数是列,即系列。使用 applymap
代替:
outputData[zfill_cols] = outputData[zfill_cols].applymap(shortenlength)