Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been using matplotlib for quite some time now and it is great however, I want to switch to panda and my first attempt at it didn't go so well.

My data set looks like this:

sam,123,184,2.6,543
winter,124,284,2.6,541
summer,178,384,2.6,542
summer,165,484,2.6,544
winter,178,584,2.6,545
sam,112,684,2.6,546
zack,145,784,2.6,547
mike,110,984,2.6,548
etc.....

I want first to search the csv for anything with the name mike and create it own list. Now with this list I want to be able to do some math for example add sam[3] + winter[4] or sam[1]/10. The last part would be to plot it columns against each other.

Going through this page

http://pandas.pydata.org/pandas-docs/stable/io.html#io-read-csv-table

The only thing I see is if I have a column header, however, I don't have any headers. I only know the position in a row of the values I want.

So my question is:

  1. How do I create a bunch of list for each row (sam, winter, summer)
  2. Is this method efficient if my csv has millions of data point?
  3. Could I use matplotlib plotting to plot pandas dataframe?

ie :

fig1 = plt.figure(figsize= (10,10))
ax = fig1.add_subplot(211)
ax.plot(mike[1], winter[3], label='Mike vs Winter speed', color = 'red')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
180 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can read a csv without headers:

data=pd.read_csv(filepath, header=None)

Columns will be numbered starting from 0. Selecting and filtering:

all_summers = data[data[0]=='summer']

If you want to do some operations grouping by the first column, it will look like this:

data.groupby(0).sum()
data.groupby(0).count()
...

Selecting a row after grouping:

sums = data.groupby(0).sum()
sums.loc['sam']

Plotting example:

 sums.plot()
 import matplotlib.pyplot as plt
 plt.show()

For more details about plotting, see: http://pandas.pydata.org/pandas-docs/version/0.18.1/visualization.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...