Home > Net >  Reading two values in one column (in a csv) with pandas
Reading two values in one column (in a csv) with pandas

Time:09-08

I am trying to read a csv file using pandas. The CSV file is structured as follows:


Timestamp, UTC, id, loc, spd
001, 12z, q20, "52, 13", 320
002, 13z, a32, "53, 12", 321
003, 14z, q32, "54, 11", 321
004, 15`, a43, "55, 10", 330

Then, I would like to plot the timestamp vs the spd using the following:

import pandas as pd
import matplotlib.pyplot as plt

fname = "data.csv"
data = pd.read_csv(fname,sep=",", header=None, skiprows=1)
data.columns = ["Timestamp", "UTC", "Callsign", "Position", "Speed", "Direction"]

t = data["Timestamp"]
utc = data["UTC"]
acid = data["Callsign"]
pos = data["Position"]
spd = ["Speed"]

plt.plot(t,spd)
plt.show()

I get a this error:

ValueError: x and y must have same first dimension, but have shapes (466,) and (1,).

When I try to plot the time vs utc it goes exactly as required. I suspect it has to do with the fact that the "loc" column has two values in one column, how would I solve this?

CodePudding user response:

After correcting the error, I was able to plot it.

The error is in this line: spd = ["Speed"] should be spd = data["Speed"]

enter image description here

But I still notice you have data such as "52 and 13", are they meant to be that way?

print(data)
   Timestamp   UTC Callsign Position Speed  Direction
0          1   12z      q20      "52   13"        320
1          2   13z      a32      "53   12"        321
2          3   14z      q32      "54   11"        321
3          4   15`      a43      "55   10"        330

CodePudding user response:

I think the error came from this spd = ["Speed"] here spd is a list of 1 as length, try:

t = data["Timestamp"]
utc = data["UTC"]
acid = data["Callsign"]
pos = data["Position"]
spd = data["Speed"] #error was here!

plt.plot(t,spd)
plt.show()
  • Related