English 中文(简体)
数据在出口数据以显示数据时没有格式
原标题:DataFrame fails formatting when exporting data to string

当我使用<<><>>>>代码>格式来分配数据值时,不适用格式。

页: 1

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),  
 )
print( string tab1 , tab1, sep= 
 )
print( 
 , end=  )
print( string tab2 , tab2, sep= 
 )

Result

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

My data is generated within a for loop. How can I format?

问题回答

缩略语为<条码>。 转而漂浮,然后进行铺设。

www.un.org/Depts/DGACM/index_spanish.htm 载于pandasv1.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),  
 )
print( string tab1 , tab1, sep= 
 )
print( 
 , end=  )
print( string tab2 , tab2, sep= 
 )

<><>Output

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     




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签