We are trying to draw graphics in the laboratory.

We have data on the frequency of the motor. How to add to the table a new column filled with the calculated values ​​from the freq2 column multiplied by 60 and bring it to the chart and call turns?

 from pathlib import Path import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates plt.style.use('ggplot') p = Path(r'C:\NET\Log\Data') df = pd.concat([pd.read_csv(f, sep=';', header=None, usecols=[2,3,5], names=['date','time',f.stem], index_col=['date','time']) for f in p.glob('*.trd')], axis=1) df = df.set_index(pd.to_datetime(df.index.get_level_values(0) + ' ' + df.index.get_level_values(1))) fig, axes = plt.subplots(3, 1, figsize=(16, 8)); plt.legend(loc = 'upper left') df.filter(regex=r'^tmp').plot(ax=axes[0]) df.filter(regex=r'^press').plot(ax=axes[1], sharex=True) df.filter(regex=r'^tok').plot(ax=axes[2], sharex=True) ax_press = df.filter(regex=r'^freq2').plot(ax=axes[2], secondary_y=True) axes[-1].xaxis.set_major_formatter(mdates.DateFormatter('%H:%M\n%d.%m.%Y')) axes[0].set_ylabel('Температура') axes[1].set_ylabel('Давление') axes[2].set_ylabel('Ток мотора') ax_press.set_ylabel('Частота') axes[-1].set_xlabel('Время') 

Data here .

    1 answer 1

    Adding a column to a DataFrame is very simple:

     In [54]: df['Обороты'] = df['freq2'] * 60 In [55]: df Out[55]: freq2 press12 press22 press42 press52 ... tmp72 tmp82 tmp92 tok_motora2 Обороты 2018-03-08 09:35:52 0.00 0.000000 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.000000 0.000000 0.0 2018-03-08 09:35:53 1.67 -0.134831 0.123405 0.012000 0.010572 ... 70.759918 144.246078 36.773521 0.000000 100.2 2018-03-08 09:35:54 1.67 -0.134831 0.123405 0.012000 0.010572 ... 70.759918 144.270630 36.773521 0.000000 100.2 2018-03-08 09:35:55 1.67 -0.140544 0.123405 0.012000 0.010572 ... 70.759918 144.270630 36.748970 0.000000 100.2 2018-03-08 09:35:56 1.67 -0.140544 0.123405 0.012000 0.010572 ... 70.759918 144.270630 36.748970 0.000000 100.2 2018-03-08 09:35:57 1.67 -0.140544 0.123405 0.009858 0.011286 ... 70.759918 144.295181 36.773521 0.000000 100.2 2018-03-08 09:35:58 1.67 -0.140544 0.123405 0.009858 0.011286 ... 70.759918 144.295181 36.773521 0.000000 100.2 2018-03-08 09:35:59 1.67 -0.139830 0.123405 0.009858 0.011286 ... 70.759918 144.295181 36.773521 0.000000 100.2 2018-03-08 09:36:00 1.67 -0.139830 0.122691 0.012000 0.011286 ... 70.759918 144.319702 36.773521 0.000000 100.2 2018-03-08 09:36:01 1.67 -0.139830 0.122691 0.012000 0.011286 ... 70.759918 144.319702 36.773521 0.000000 100.2 ... ... ... ... ... ... ... ... ... ... ... ... 2018-03-08 14:31:24 49.00 0.549742 0.452620 0.021998 0.024140 ... 73.536102 141.545776 111.866852 77.400002 2940.0 2018-03-08 14:31:25 49.00 0.549742 0.452620 0.020570 0.023426 ... 73.536102 141.545776 111.866852 77.400002 2940.0 2018-03-08 14:31:26 49.00 0.549742 0.452620 0.020570 0.023426 ... 73.536110 141.545776 111.866852 77.400002 2940.0 2018-03-08 14:31:27 49.00 0.551170 0.452620 0.020570 0.023426 ... NaN 141.545776 111.866852 77.400002 2940.0 2018-03-08 14:31:34 49.00 NaN NaN NaN NaN ... NaN NaN NaN NaN 2940.0 2018-03-08 14:31:35 NaN 0.551170 0.452620 0.020570 0.023426 ... 73.536110 141.521225 111.866852 NaN NaN 2018-03-08 14:31:36 49.00 0.549742 0.451906 0.021998 0.023426 ... 73.527199 141.545776 111.891403 NaN 2940.0 2018-03-08 14:31:37 49.00 0.549742 0.451906 0.021284 0.023426 ... 73.527199 141.521225 111.891403 NaN 2940.0 2018-03-08 14:31:38 49.00 NaN NaN 0.021284 0.023426 ... 73.527191 141.521225 NaN NaN 2940.0 2018-03-08 14:31:54 NaN 0.549742 0.451906 NaN NaN ... NaN NaN 111.866852 NaN NaN 

    a few more ways to add a column based on another column:

    df.assign () :

     df = df.assign(Обороты=df['freq2'] * 60) 

    df.eval ()

     df = df.eval("Обороты = freq2 * 60") 

    PS how to draw graphics you already know ;)

    • damn it - they almost did it :) - only for some reason df.assign was taken! Thank you - you save once again! - Ivan
    • one
      @Ivan, you can use df.assign : df = df.assign(Обороты=df['freq2'] * 60) - MaxU
    • Hooray - it turned out :) - Ivan