I want to remove the specific data, please, let me know how to make code. I already coded how to change .txt into .csv.
Let me know what I should do to remove line that '0' inclued
I want to remove the specific data, please, let me know how to make code. I already coded how to change .txt into .csv.
Let me know what I should do to remove line that '0' inclued
I hope you find it useful.
data=[]
for line in open('text.txt', 'r').readlines():
columns = line.split(':')
if 'error' in columns[0] or len(columns)==1:
data.append(float(columns[-1].replace('\n','')))
print(data)
You can solve it with several lines of code (edited: question modified data):
import pandas as pd
import numpy as np
df = pd.read_csv('data.txt', delim_whitespace=True, header=None)
df['result'] = df.apply(lambda row: [x for x in row if 'error:' not in str(x)][0], axis=1)
df['result'] = df['result'].apply(lambda x: np.nan if ':' in str(x) else x)
df[df['result'].isna()==False]['result']
Explanation: First read the ORIGINAL file with space/s as delimiter.
import pandas as pd
import numpy as np
df = pd.read_csv('data.txt', delim_whitespace=True, header=None)
df:
0 1
0 error: 0.007302
1 time: 240.835871
2 evaluations: 12185.000000
3 parameters: NaN
4 2.26024234659371134e-04 NaN
5 6.99262803719691711e-02 NaN
6 3.44898647418988432e-05 NaN
7 5.46135549515059243e-02 NaN
8 8.73320929720244749e-02 NaN
9 8.93333608554642590e-03 NaN
10 5.14965100242248744e-03 NaN
11 3.15603139095758239e-02 NaN
12 1.52425084606730654e-01 NaN
Now get the values in first columna of a row without "error:" as result:
df['result'] = df.apply(lambda row: [x for x in row if 'error:' not in str(x)][0], axis=1)
df:
0 1 result
0 error: 0.007302 0.00730238
1 time: 240.835871 time:
2 evaluations: 12185.000000 evaluations:
3 parameters: NaN parameters:
4 2.26024234659371134e-04 NaN 2.26024234659371134e-04
5 6.99262803719691711e-02 NaN 6.99262803719691711e-02
6 3.44898647418988432e-05 NaN 3.44898647418988432e-05
7 5.46135549515059243e-02 NaN 5.46135549515059243e-02
8 8.73320929720244749e-02 NaN 8.73320929720244749e-02
9 8.93333608554642590e-03 NaN 8.93333608554642590e-03
10 5.14965100242248744e-03 NaN 5.14965100242248744e-03
11 3.15603139095758239e-02 NaN 3.15603139095758239e-02
12 1.52425084606730654e-01 NaN 1.52425084606730654e-01
Let's insert nan in results with a ':' character.
df['result'] = df['result'].apply(lambda x: np.nan if ':' in str(x) else x)
df:
0 1 result
0 error: 0.007302 0.00730238
1 time: 240.835871 NaN
2 evaluations: 12185.000000 NaN
3 parameters: NaN NaN
4 2.26024234659371134e-04 NaN 2.26024234659371134e-04
5 6.99262803719691711e-02 NaN 6.99262803719691711e-02
6 3.44898647418988432e-05 NaN 3.44898647418988432e-05
7 5.46135549515059243e-02 NaN 5.46135549515059243e-02
8 8.73320929720244749e-02 NaN 8.73320929720244749e-02
9 8.93333608554642590e-03 NaN 8.93333608554642590e-03
10 5.14965100242248744e-03 NaN 5.14965100242248744e-03
11 3.15603139095758239e-02 NaN 3.15603139095758239e-02
12 1.52425084606730654e-01 NaN 1.52425084606730654e-01
And get results droping the nan rows:
df[df['result'].isna()==False]['result']
0 0.00730238
4 2.26024234659371134e-04
5 6.99262803719691711e-02
6 3.44898647418988432e-05
7 5.46135549515059243e-02
8 8.73320929720244749e-02
9 8.93333608554642590e-03
10 5.14965100242248744e-03
11 3.15603139095758239e-02
12 1.52425084606730654e-01
Name: result, dtype: object
And to avoid writing the '0' user header=False:
df.to_csv(f'{f[:-4]}.txt', index=False, header=False)