I have this below data file and I want three columns with heading “TIMESTEP”, “id” and “mass”.
Its corresponding values are just immediately below itenter image description here. How to do it. Please help
Below link 1 is my snapshot of data file and 2 is my desired arrangement.
Advertisement
Answer
I agree with the comments that the question is hard to understand, but from my understanding of your problem I have found this solution:
JavaScript
x
34
34
1
import pandas as pd
2
3
data_input = """TIMESTEP
4
5000
5
id mass
6
TIMESTEP
7
5100
8
id mass
9
42 24
10
TIMESTEP
11
5200
12
id mass
13
99 123
14
32 84
15
79 424"""
16
17
columns=["TIMESTEP", "id", "mass"]
18
data = []
19
previous_line = ""
20
21
for line in data_input.split("n"):
22
if columns[0] in previous_line and columns[1] not in line:
23
data.append({"TIMESTEP": line})
24
elif columns[1] in previous_line and columns[0] not in line:
25
data[len(data)-1]["id"], data[len(data)-1]["mass"] = line.split(" ")
26
elif all(col not in line for col in columns):
27
data.append({"TIMESTEP": data[len(data)-1]["TIMESTEP"]})
28
data[len(data)-1]["id"], data[len(data)-1]["mass"] = line.split(" ")
29
30
previous_line = line
31
32
df = pd.DataFrame(data)
33
print(df)
34
Try to run this script and see if this is what you were looking for.