anon01
anon01

Reputation: 11171

call pandas plot function for path entry

I have a pandas dataframe that holds the file path to .wav data. Can I use pandas DataFrame.plot() function to plot the data referenced?

Example:

typical usage: df.plot()

what I'm trying to do: df.plot(df.path_to_data)???

I suspect some combination of apply and lambda will do the trick, but I'm not very familiar with these tools.

Upvotes: 0

Views: 295

Answers (2)

jezrael
jezrael

Reputation: 862701

I think you need first create DataFrame obviously by read_csv and then DataFrame.plot:

pd.read_csv('path_to_data').plot()

But if need plot DataFrames created from paths from column in DataFrame:

df.path_to_data.apply(lambda x: pd.read_csv(x).plot())

Or use custom function:

def f(x):
    pd.read_csv(x).plot()

df.path_to_data.apply(f)

Or use loop:

for x in df.path_to_data:
    pd.read_csv(x).plot()

Upvotes: 1

cs95
cs95

Reputation: 402543

No, that isn't possible. plot is first order function that operates on pd.DataFrame objects. Here, df would be the same thing. What you'd need to do is

  1. Load your dataframe using pd.read_* (usually, pd.read_csv(file)) and assign to df
  2. Now call df.plot

So, in summary, you need -

df = pd.read_csv(filename)
... # some processing here (if needed)
df.plot()

As for the question of whether this can be done "without loading data in memory"... you can't plot data that isn't in memory. If you want to, you can limit tha number of rows you read, or you can load it efficiently, by loading it in chunks. You can also write code to aggregate/summarise data, or sample it.

Upvotes: 1

Related Questions