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:
import pandas as pd
data_input = """TIMESTEP
5000
id mass
TIMESTEP
5100
id mass
42 24
TIMESTEP
5200
id mass
99 123
32 84
79 424"""
columns=["TIMESTEP", "id", "mass"]
data = []
previous_line = ""
for line in data_input.split("n"):
if columns[0] in previous_line and columns[1] not in line:
data.append({"TIMESTEP": line})
elif columns[1] in previous_line and columns[0] not in line:
data[len(data)-1]["id"], data[len(data)-1]["mass"] = line.split(" ")
elif all(col not in line for col in columns):
data.append({"TIMESTEP": data[len(data)-1]["TIMESTEP"]})
data[len(data)-1]["id"], data[len(data)-1]["mass"] = line.split(" ")
previous_line = line
df = pd.DataFrame(data)
print(df)
Try to run this script and see if this is what you were looking for.