I'm a 'newbie' in python (started learning 2 weeks ago) and I'm trying to plot a file that looks like this:
"1stSerie"
2 23
4 12
6 12
"2ndSerie"
2 51
4 90
6 112
Using any of the following: pandas, matplotlib and numpy. But I am not having much of a success. I tried searching for examples but none applied to my data format.
Can somebody help me find out how to load this file in a pandas dataframe or (what would be even better) show me how to plot this?
Details:
- The number of lines in each series is different for different datasets that I have, but in the same dataset(file) the number of rows are the same (as pictured in the code excerpt).
- There is a blank line between each serie exactly as reproduced in the code excerpt.
- The title of the serie is the whole string, but with one word (if it is easier to import using two words/colums) I can change the titles.
UPDATE 1:
After the help from @Goyo I changed my method convert()
to be something like this:
#!/usr/bin/env python3
def convert(in_file, out_file):
name = ""
for line in in_file:
line = line.strip()
print(line)
if line == "":
continue
if line.startswith('"'):
name = line.strip('"')
print("NAME:: " + name)
else:
out_file.write("{0}\n".format(','.join([name] + line.split("\t")) ) )
To plot I'm using the following code:
with open('nro_caribou.dat') as in_file:
with open('output.txt', 'w+') as out_file:
convert(in_file, out_file)
df = pd.read_csv('output.txt', header=None,names=['Methods', 'Param', 'Time'], sep=",", )
print(df)
df.pivot(values='Time', index='Param', columns='Methods').plot()
My original data: https://gist.github.com/pedro-stanaka/c3eda0aa2191950a8d83
And my plot:
"1st Serie"
and"1st Serie"
and then3
rows of data. Has this file constant number of rows of data? Are thisseries
separated byempty row
? – jezrael Feb 14 at 17:40Names
ofSeries
are1st Series
and2nd Series
or can bea
,b
, it means is not created by two string -1st
andSeries
? – jezrael Feb 14 at 18:01