pandas 将数据导出为字符串时,DataFrame格式化失败

rlcwz9us  于 2023-09-29  发布在  其他
关注(0)|答案(1)|浏览(93)

当我使用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循环中生成的。如何格式化?

bzzcjhmw

bzzcjhmw1#

df1中的列是object。在格式化为字符串之前转换为浮点数。

在pandas v1.5.1上测试。

print(df1.dtypes)
print("Before" + '-'*10)
print(df2.dtypes)
df2 = df2.astype(float)
print("After" + '-'*10)
print(df2.dtypes)

print('-'*10)

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')

输出

x      int64                                                                                                   
a    float64                                                                                                   
b    float64                                                                                                   
c    float64                                                                                                   
z    float64                                                                                                   
w    float64                                                                                                   
dtype: object                                                                                                  
Before----------                                                                                               
x     int64                                                                                                    
a    object                                                                                                    
b    object                                                                                                    
c    object                                                                                                    
z    object                                                                                                    
w    object                                                                                                    
dtype: object                                                                                                  
After----------                                                                                                
x    float64                                                                                                   
a    float64                                                                                                   
b    float64                                                                                                   
c    float64                                                                                                   
z    float64                                                                                                   
w    float64                                                                                                   
dtype: object                                                                                                  
----------                                                                                                     
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.0   3.02 113.3 132.3 2.17 2.19                                                                            
1  2.0   9.10 113.9 132.7 2.16 2.22                                                                            
2  3.0  27.46 115.4 133.5 2.13 2.31                                                                            
3  4.0  82.86 118.9 135.4 2.07 2.60                                                                            
4  5.0 250.00 120.4 134.5 1.98 2.99

相关问题