-2

I have a dataset like the one shown below.

Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3
16/12/2006;17:24:00;4.216;0.418;234.840;18.400;0.000;1.000;17.000
16/12/2006;17:25:00;5.360;0.436;233.630;23.000;0.000;1.000;16.000
16/12/2006;17:26:00;5.374;0.498;233.290;23.000;0.000;2.000;17.000
16/12/2006;17:27:00;5.388;0.502;233.740;23.000;0.000;1.000;17.000
16/12/2006;17:28:00;3.666;0.528;235.680;15.800;0.000;1.000;17.000
16/12/2006;17:29:00;3.520;0.522;235.020;15.000;0.000;2.000;17.000
16/12/2006;17:30:00;3.702;0.520;235.090;15.800;0.000;1.000;17.000
16/12/2006;17:31:00;3.700;0.520;235.220;15.800;0.000;1.000;17.000
16/12/2006;17:32:00;3.668;0.510;233.990;15.800;0.000;1.000;17.000

I've used pandas to get the data into a DataFrame. The dataset has data for multiple days with an interval of 1 min for each row in the dataset.

I want to plot separate graphs for the voltage with respect to the time(shown in column 2) for each day(shown in column 1) using python. How can I do that?

krishna
  • 405
  • 6
  • 25

4 Answers4

1
txt = '''Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3
16/12/2006;17:24:00;4.216;0.418;234.840;18.400;0.000;1.000;17.000
16/12/2006;17:25:00;5.360;0.436;233.630;23.000;0.000;1.000;16.000
16/12/2006;17:26:00;5.374;0.498;233.290;23.000;0.000;2.000;17.000
16/12/2006;17:27:00;5.388;0.502;233.740;23.000;0.000;1.000;17.000
16/12/2006;17:28:00;3.666;0.528;235.680;15.800;0.000;1.000;17.000
16/12/2006;17:29:00;3.520;0.522;235.020;15.000;0.000;2.000;17.000
16/12/2006;17:30:00;3.702;0.520;235.090;15.800;0.000;1.000;17.000
16/12/2006;17:31:00;3.700;0.520;235.220;15.800;0.000;1.000;17.000
16/12/2006;17:32:00;3.668;0.510;233.990;15.800;0.000;1.000;17.000'''

from io import StringIO
f = StringIO(txt)
df = pd.read_table(f,sep =';' )
plt.plot(df['Time'],df['Voltage'])
plt.show()

gives output : enter image description here

Krishna
  • 6,107
  • 2
  • 40
  • 43
1

I believe this will do the trick (I edited the dates so we have two dates)

import pandas as pd
import matplotlib.pyplot as plt

%matplotlib inline #If you use Jupyter Notebook

df = pd.read_csv('test.csv', sep=';', usecols=['Date','Time','Voltage'])
unique_dates = df.Date.unique()
for date in unique_dates:
   print('Date: ' + date)
   df.loc[df.Date == date].plot.line('Time', 'Voltage')
   plt.show()

You will get this:

enter image description here

Yannis
  • 683
  • 7
  • 16
  • I got an error when i tried this. `sys:1: DtypeWarning: Columns (4) have mixed types. Specify dtype option on import or set low_memory=False. ` – krishna Jul 21 '18 at 17:28
  • https://stackoverflow.com/questions/24251219/pandas-read-csv-low-memory-and-dtype-options – Yannis Jul 23 '18 at 22:30
0
X = df.Date.unique() 

for i in X: #iterate over unique days
    temp_df = df[df.Date==i] #get df for specific day
    temp_df.plot(x = 'Time', y = 'Voltage') #plot

If you want to change x values you can use

x = np.arange(1, len(temp_df.Time), 1)
AnotherLazyPeon
  • 131
  • 1
  • 1
  • 9
0

group by hour and minute after creating a DateTime variable to handle multiple days. you can filter the grouped for a specific day.

txt = '''Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3 16/12/2006;17:24:00;4.216;0.418;234.840;18.400;0.000;1.000;17.000 16/12/2006;17:25:00;5.360;0.436;233.630;23.000;0.000;1.000;16.000 16/12/2006;17:26:00;5.374;0.498;233.290;23.000;0.000;2.000;17.000 16/12/2006;17:27:00;5.388;0.502;233.740;23.000;0.000;1.000;17.000 16/12/2006;17:28:00;3.666;0.528;235.680;15.800;0.000;1.000;17.000 16/12/2006;17:29:00;3.520;0.522;235.020;15.000;0.000;2.000;17.000 16/12/2006;17:30:00;3.702;0.520;235.090;15.800;0.000;1.000;17.000 16/12/2006;17:31:00;3.700;0.520;235.220;15.800;0.000;1.000;17.000 16/12/2006;17:32:00;3.668;0.510;233.990;15.800;0.000;1.000;17.000'''

 from io import StringIO
 f = StringIO(txt)
 df = pd.read_table(f,sep =';' )

 df['DateTime']=pd.to_datetime(df['Date']+"T"+df['Time']+"Z")
 df.set_index('DateTime',inplace=True)

 filter=df['Date']=='16/12/2006'
 grouped=df[filter].groupby([df.index.hour,df.index.minute])['Voltage'].mean()
 grouped.plot()
 plt.show()
Golden Lion
  • 3,840
  • 2
  • 26
  • 35