Issue
Python newbie here, I have a file which contains different physical experiment, divided by a blank line, as:
#t x
0 0
0 1
0 2
0 1
0 2
0 3
i would like to know if there is an easy way to split my data in multiple lists, based off column and experiment (list t1 for times of the first experiment, etc..), so that i can plot different experiments separately.
I tried with numpy.loadtxt and pandas.read_csv but they just skip over the blank lines and merge all the data. I searched for something like the c fscanf function that stops at a blank line but i couldn`t find an easy way.
Solution
There is no direct way to split while reading the data, you can however read the whole file and take advantage of the blank lines to split the dataframe:
df = pd.read_csv('your_file.csv', sep=r'\s+', skip_blank_lines=False)
m = df.isna().all(axis=1)
dfs = [g for _,g in df[~m].groupby(m.cumsum())]
print(dfs)
Output:
[ #t x # DataFrame #1
0 0.0 0.0
1 0.0 1.0
2 0.0 2.0,
#t x # DataFrame #2
4 0.0 1.0
5 0.0 2.0
6 0.0 3.0]
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.