I want to manipulate data of text file either in MATLAB or python. My data file contains 3000 rows but I have posted here for just as an example of 4 rows of data. The data file has R, L, G, C data for different frequencies (here 3 frequency in 3 rows). Now I want to manipulate the data to another format (Let's say, the target file).
Now, Can anyone help me with what is the command or loop command that I need for manipulate the data for 3000 frequencies and at the end, to save it as new target text file.
The data file is:
FORMAT Freq R1:1 R1:2 R2:1 R2:2 L1:1 L1:2 L2:1 L2:2 G1:1 G1:2 G2:1 G2:2 C1:1 C1:2 C2:1 C2:2
0.00E+00 5.11E+00 7.13E-13 7.13E-13 5.11E+00 6.61E-07 4.46E-07 4.46E-07 6.61E-07 1.04E-17 -5.02E-18 -5.02E-18 1.04E-17 5.58E-11 -3.27E-11 -3.27E-11 5.58E-11
1.00E+06 5.59E+00 9.82E-03 9.82E-03 5.59E+00 6.97E-07 4.47E-07 4.47E-07 6.97E-07 6.42E-07 -3.11E-07 -3.11E-07 6.42E-07 5.54E-11 -3.25E-11 -3.25E-11 5.54E-11
2.00E+06 5.69E+00 2.47E-02 2.47E-02 5.69E+00 7.06E-07 4.48E-07 4.48E-07 7.06E-07 1.29E-06 -6.23E-07 -6.23E-07 1.29E-06 5.53E-11 -3.25E-11 -3.25E-11 5.53E-11
and target file is:
FORMAT Freq : R1:1 R1:2 R2:1 R2:2
L1:1 L1:2 L2:1 L2:2
G1:1 G1:2 G2:1 G2:2
C1:1 C1:2 C2:1 C2:2
0.00E+00 : 5.11E+00 7.13E-13 7.13E-13 5.11E+00
6.61E-07 4.46E-07 4.46E-07 6.61E-07
1.04E-17 -5.02E-18 -5.02E-18 1.04E-17
5.58E-11 -3.27E-11 -3.27E-11 5.58E-11
1.00E+06 : 5.59E+00 9.82E-03 9.82E-03 5.59E+00
6.97E-07 4.47E-07 4.47E-07 6.97E-07
6.42E-07 -3.11E-07 -3.11E-07 6.42E-07
5.54E-11 -3.25E-11 -3.25E-11 5.54E-11
2.00E+06 : 5.69E+00 2.47E-02 2.47E-02 5.69E+00
7.06E-07 4.48E-07 4.48E-07 7.06E-07
1.29E-06 -6.23E-07 -6.23E-07 1.29E-06
5.53E-11 -3.25E-11 -3.25E-11 5.53E-11
I am using Anaconda (Spyder) for python package (which includes python 3.6.4). The code looks like what @xg.plt.py suggested which gives error because data is stored as strings. So I tried to change data type in different ways and it is giving me different errors. My code is given below:
import pandas as pd
df = pd.read_csv('Data_file.txt')
#df = df.apply(pd.to_numeric, errors='ignore')
#df = df.apply(pd.to_numeric, errors='coerce')
#df = df.apply(pd.to_numeric)
#df = df.infer_objects()
df = df.astype(float)
formatters = {col : ('{:.2e}\n'.format if col[-3:]=='2:2' else '{:.2e}'.format) for col in df.columns}
formatters[df.columns[0]] = '{:.2e} : '.format
header_formatters = {col : ('{} \n'.format if col[-3:]=='2:2' else '{} '.format) for col in df.columns}
header_formatters[df.columns[0]] = '{} : '.format
f = open('formatted.txt','w')
f.write(''.join([header_formatters[col](col) for col in df.columns])+'\n')
df.to_string(buf=f,formatters=formatters,header=False,index=False)
f.close()
If I don't change data type it gives me error like below:
ValueError: Unknown format code 'e' for object of type 'str'
If I use df = df.apply(pd.to_numeric, errors='ignore')
or df = df.infer_objects()
it gives me same error like above ValueError: Unknown format code 'e' for object of type 'str'
If I use df = df.apply(pd.to_numeric, errors='coerce')
it runs but it coerce all the value to NaN in first column and results in formatted file like below:
FORMAT Freq R1:1 R1:2 R2:1 R2:2 L1:1 L1:2 L2:1 L2:2 G1:1 G1:2 G2:1 G2:2 C1:1 C1:2 C2:1 C2:2 :
nan :
nan :
nan :
if I use df = df.apply(pd.to_numeric)
it give me following error:
ValueError: ('Unable to parse string "0.00E+00\t5.11E+00\t7.13E-13\t7.13E-13\t5.11E+00\t6.61E-07\t4.46E-07\t4.46E-07\t6.61E-07\t1.04E-17\t-5.02E-18\t-5.02E-18\t1.04E-17\t5.58E-11\t-3.27E-11\t-3.27E-11\t5.58E-11" at position 0', 'occurred at index FORMAT Freq\tR1:1\tR1:2\tR2:1\tR2:2\tL1:1\tL1:2\tL2:1\tL2:2\tG1:1\tG1:2\tG2:1\tG2:2\tC1:1\tC1:2\tC2:1\tC2:2')
When I type df.dtypes
to check the dataframe type it gives me following result:
FORMAT Freq\tR1:1\tR1:2\tR2:1\tR2:2\tL1:1\tL1:2\tL2:1\tL2:2\tG1:1\tG1:2\tG2:1\tG2:2\tC1:1\tC1:2\tC2:1\tC2:2 float64
dtype: object
if I use df = df.astype(float)
it give me following error:
ValueError: could not convert string to float: '2.00E+06\t5.69E+00\t2.47E-02\t2.47E-02\t5.69E+00\t7.06E-07\t4.48E-07\t4.48E-07\t7.06E-07\t1.29E-06\t-6.23E-07\t-6.23E-07\t1.29E-06\t5.53E-11\t-3.25E-11\t-3.25E-11\t5.53E-11'