Let's say I've got this csv file that I want to import and graph in python using pyplot and pandas.
1,2
2,4
3,3
4,4
5,6
6,3
7,5
8,6
1,3
2,5
3,7
4,4
5,3
6,5
7,4
8,5
1,3
2,2
3,5
4,4
5,3
6,5
7,6
8,7
As you can see, column 1 rolls over at the number 8. How can I get rid of this rollover, such that it now looks like this:
1,2
2,4
3,3
4,4
5,6
6,3
7,5
8,6
9,3
10,5
11,7
12,4
13,3
14,5
15,4
16,5
17,3
18,2
19,5
20,4
21,3
22,5
23,6
24,7
I have tried a for loop to search through the column and keep track of every time it finds a number that was smaller than the last...that must mean a rollover! I'm looping over the whole dataset (which is 95,000 elements!) and when I see that the current item is larger than the last, i multiply it by a counter...The counter get's incremented when this is wrong and I add it to the current entry until another rollover is detected.
But, i'm doing something wrong, and i'm not sure what..My indices are getting screwed up at the ends.. What is the Pythonic way to search through this mess?
Let's not even import in that first column and let the index of the default range index of the dataframe act as your x-axis with pandas df.plot.
from io import StringIO
csv_file = StringIO("""
1,2
2,4
3,3
4,4
5,6
6,3
7,5
8,6
1,3
2,5
3,7
4,4
5,3
6,5
7,4
8,5
1,3
2,2
3,5
4,4
5,3
6,5
7,6
8,7""")
df = pd.read_csv(csv_file, header=None, usecols=[1])
df.plot()
Output:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With