1

I am new to plotting charts in python. I've been told to use Pandas for that, using the following command. Right now it is assumed the csv file has headers (time,speed, etc). But how can I change it to when the csv file doesn't have headers? (data starts from row 0)

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df  = pd.read_csv("P1541350772737.csv")
#df.head(5)
df.plot(figsize=(15,5), kind='line',x='timestamp', y='speed') # scatter plot
2
  • 1
    Skip the plotting for the moment, and focus on what pandas loads. Look at the df. Seek to understand how it labels rows and columns. Commented Nov 7, 2018 at 20:04
  • yes, definitely I need to learn it. Any preference to use matplotlib for plotting instead of Panda's own plot?! Commented Nov 7, 2018 at 20:07

5 Answers 5

2

You can specify x and y by the index of the columns, you don't need names of the columns for that:

Very simple: df.plot(figsize=(15,5), kind='line',x=0, y=1)

It works if x column is first and y column is second and so on, columns are numerated from 0

For example:

enter image description here

The same result with the names of the columns instead of positions: enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

How can I pass only the index?! This will be simple!
I added to the answer
ummm it doesn't work for me! x=0 simply shows a (0,1) line!
it depends on what you want to plot. x=0 just indicates that the column that comes first should be on the x axis. If this column had a name you could write it's name instead of it's index (i.e. position). Note that if you plot with kind='line' at least one of columns (x or y) have to be numeric.
1

I may havve missinterpreted your question but II'll do my best.

Th problem seems to be that you have to read a csv that have no header but you want to add them. I would use this code:

cols=['time', 'speed', 'something', 'else'] 
df = pd.read_csv('useful_data.csv', names=cols, header=None)

For your plot, the code you used should be fine with my correction. I would also suggest to look at matplotlib in order to do your graph.

3 Comments

does the cols length need to match with the total number of columns in the csv?! It doesn't work right now.
Yes, it must match
Looks like it doesn't have to match!
1

You can try

df  = pd.read_csv("P1541350772737.csv", header=None)

with the names-kwarg you can set arbitrary column headers, this implies silently headers=None, i.e. reading data from row 0.

You might also want to check the doc https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

1 Comment

Then how to specify x and y?
1

Pandas is more focused on data structures and data analysis tools, it actually supports plotting by using Matplotlib as backend. If you're interested in building different types of plots in Python you might want to check it out.

Back to Pandas, Pandas assumes that the first row of your csv is a header. However, if your file doesn't have a header you can pass header=None as a parameter pd.read_csv("P1541350772737.csv", header=None) and then plot it as you are doing it right now.

The full list of commands that you can pass to Pandas for reading a csv can be found at Pandas read_csv documentation, you'll find a lot of useful commands there (such as skipping rows, defining the index column, etc.)

Happy coding!

Comments

1

For most commands you will find help in the respective documentation. Looking at pandas.read_csv you'll find an argument names

names : array-like, default None
List of column names to use. If file contains no header row, then you should explicitly pass header=None.

So you will want to give your columns names by which they appear in the dataframe.

As an example: Suppose you have this data file

1, 2
3, 4
5, 6

Then you can do

import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("data.txt", names=["A", "B"], header=None)
print(df)
df.plot(x="A", y="B")

plt.show()

which outputs

   A  B
0  1  2
1  3  4
2  5  6

enter image description here

2 Comments

does it matter to delete plt.show()?! Looks like no changes!
This totally depends on where and how you run the code. Some programs call that automatically for you so to speak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.