当我使用for
循环分配数据值时,不应用格式化。
Python 3.10.6; Pandas 1.4.4;简体中文(zh_cn)
from pandas import DataFrame as DF
import numpy as np
data = np.array([[3.017088, 113.310384, 132.345828, 2.165728, 2.185373],
[9.102821, 113.860827, 132.66552, 2.157181, 2.216547],
[27.464014, 115.400822, 133.546328, 2.132698, 2.312204],
[82.861350, 118.902896, 135.400819, 2.070627, 2.59839],
[250.000000, 120.362021, 134.470661, 1.982228, 2.992826]])
df1 = DF(columns=['x', 'a', 'b', 'c', 'z', 'w'])
df2 = DF(columns=['x', 'a', 'b', 'c', 'z', 'w'])
df1['x'] = range(1, 6)
df2['x'] = range(1, 6)
df1.iloc[:, 1:] = data
for i, line in enumerate(data):
df2.loc[i, 'a':'w'] = line
forms = {'a': lambda v: f'{v:.2f}',
'b': lambda v: f'{v:.1f}',
'c': lambda v: f'{v:.1f}',
'z': lambda v: f'{v:.2f}',
'w': lambda v: f'{v:.2f}'}
tab1 = df1.to_string(na_rep='-', formatters=forms)
tab2 = df2.to_string(na_rep='-', formatters=forms)
print(all(df1 == df2), '\n')
print('string tab1', tab1, sep='\n')
print('\n', end='')
print('string tab2', tab2, sep='\n')
结果
True
string tab1
x a b c z w
0 1 3.02 113.3 132.3 2.17 2.19
1 2 9.10 113.9 132.7 2.16 2.22
2 3 27.46 115.4 133.5 2.13 2.31
3 4 82.86 118.9 135.4 2.07 2.60
4 5 250.00 120.4 134.5 1.98 2.99
string tab2
x a b c z w
0 1 3.017088 113.310384 132.345828 2.165728 2.185373
1 2 9.102821 113.860827 132.66552 2.157181 2.216547
2 3 27.464014 115.400822 133.546328 2.132698 2.312204
3 4 82.86135 118.902896 135.400819 2.070627 2.59839
我的数据是在for
循环中生成的。如何格式化?
1条答案
按热度按时间bzzcjhmw1#
df1中的列是
object
。在格式化为字符串之前转换为浮点数。在pandas
v1.5.1
上测试。输出