Skip to content
Snippets Groups Projects
Commit ac86373a authored by Francesco Santanastasio's avatar Francesco Santanastasio
Browse files

fix problem with drop

parent 4951f96e
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:a668e702 tags:
Esempio di lettura di un file di test dell'esperimento "Volano". Il numero di cifre significative nei dati sono arbitrarie. Lo scopo è solo di illustrare alcune funzioni utili di python per lavorare i dati.
%% Cell type:code id:2c3314df tags:
``` python
#plots will be shown inline
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
from scipy import stats
import pandas as pd
```
%% Cell type:code id:1313521e tags:
``` python
# sostituire la virgola con il punto
reading_file = open("dati_volano_raw.txt", "r")
new_file_content = ""
for line in reading_file:
stripped_line = line.strip()
new_line = stripped_line.replace(",", ".")
new_file_content += new_line +"\n"
reading_file.close()
writing_file = open("dati_volano.txt", "w")
writing_file.write(new_file_content)
writing_file.close()
```
%% Cell type:code id:7d55cb2e tags:
``` python
df = pd.read_csv('dati_volano.txt',header=1,sep='\t') #il separatore in questo caso era un "tab" (\t)
df
```
%% Output
Time (s) Angle (rad) Angular Velocity (rad/s) \
0 0.02 0.000000 NaN
1 0.04 0.000000 0.00000
2 0.06 0.000000 0.00000
3 0.08 0.000000 0.00000
4 0.10 0.000000 0.00000
... ... ... ...
4805 96.12 -9.501754 -0.03927
4806 96.14 -9.501754 0.00000
4807 96.16 -9.501754 0.00000
4808 96.18 -9.501754 0.00000
4809 96.20 -9.501754 0.00000
Angular Acceleration (rad/s²) Position (m) Velocity (m/s) \
0 NaN 0.000000 NaN
1 NaN 0.000000 0.000000
2 0.000000 0.000000 0.000000
3 0.000000 0.000000 0.000000
4 0.000000 0.000000 0.000000
... ... ... ...
4805 0.981748 -0.226837 -0.000937
4806 0.981748 -0.226837 0.000000
4807 0.000000 -0.226837 0.000000
4808 0.000000 -0.226837 0.000000
4809 NaN -0.226837 0.000000
Acceleration (m/s²)
0 NaN
1 NaN
2 0.000000
3 0.000000
4 0.000000
... ...
4805 0.023438
4806 0.023438
4807 0.000000
4808 0.000000
4809 NaN
[4810 rows x 7 columns]
%% Cell type:code id:7eb3c699 tags:
``` python
# remove lines with "NaN" (Not a Number)
df = df.dropna()
df
```
%% Output
Time (s) Angle (rad) Angular Velocity (rad/s) \
2 0.06 0.000000 0.00000
3 0.08 0.000000 0.00000
4 0.10 0.000000 0.00000
5 0.12 0.000000 0.00000
6 0.14 0.000000 0.00000
... ... ... ...
4804 96.10 -9.500183 -0.03927
4805 96.12 -9.501754 -0.03927
4806 96.14 -9.501754 0.00000
4807 96.16 -9.501754 0.00000
4808 96.18 -9.501754 0.00000
Angular Acceleration (rad/s²) Position (m) Velocity (m/s) \
2 0.000000 0.000000 0.000000
3 0.000000 0.000000 0.000000
4 0.000000 0.000000 0.000000
5 0.000000 0.000000 0.000000
6 0.000000 0.000000 0.000000
... ... ... ...
4804 -0.981748 -0.226800 -0.000937
4805 0.981748 -0.226837 -0.000937
4806 0.981748 -0.226837 0.000000
4807 0.000000 -0.226837 0.000000
4808 0.000000 -0.226837 0.000000
Acceleration (m/s²)
2 0.000000
3 0.000000
4 0.000000
5 0.000000
6 0.000000
... ...
4804 -0.023438
4805 0.023438
4806 0.023438
4807 0.000000
4808 0.000000
[4807 rows x 7 columns]
%% Cell type:code id:c9e70ccf tags:
``` python
#rinominare le colonne
df = df.rename(columns={"Time (s)": "t"
, "Angle (rad)": "angle"
, "Angular Velocity (rad/s)": "angle_v"
, "Angular Acceleration (rad/s²)": "angle_a"
, "Position (m)": "x"
, "Velocity (m/s)": "v"
, "Acceleration (m/s²)": "a"} )
df
```
%% Output
t angle angle_v angle_a x v a
2 0.06 0.000000 0.00000 0.000000 0.000000 0.000000 0.000000
3 0.08 0.000000 0.00000 0.000000 0.000000 0.000000 0.000000
4 0.10 0.000000 0.00000 0.000000 0.000000 0.000000 0.000000
5 0.12 0.000000 0.00000 0.000000 0.000000 0.000000 0.000000
6 0.14 0.000000 0.00000 0.000000 0.000000 0.000000 0.000000
... ... ... ... ... ... ... ...
4804 96.10 -9.500183 -0.03927 -0.981748 -0.226800 -0.000937 -0.023438
4805 96.12 -9.501754 -0.03927 0.981748 -0.226837 -0.000937 0.023438
4806 96.14 -9.501754 0.00000 0.981748 -0.226837 0.000000 0.023438
4807 96.16 -9.501754 0.00000 0.000000 -0.226837 0.000000 0.000000
4808 96.18 -9.501754 0.00000 0.000000 -0.226837 0.000000 0.000000
[4807 rows x 7 columns]
%% Cell type:code id:3982ec4f tags:
``` python
# fare un grafico direttamente dal dataframe
df.plot(x="t",y="x",linestyle="None",marker=".")
```
%% Output
<AxesSubplot:xlabel='t'>
%% Cell type:code id:d97129d6 tags:
``` python
#selezionare un intervallo di tempo (rimuovere alcuni righe del dataframe)
print ("Numero misure (iniziali): ",len(df))
df_new = df.drop(df[df.t <20 ].index | df[df.t > 80].index)
index_to_be_drop = df[ (df.t<20) | (df.t>80) ].index
#df_new = df.drop(df[df.t <20 ].index | df[df.t > 80].index) #obsolete (and not working in some cases)
df_new = df.drop(index_to_be_drop)
print ("Numero misure (dopo i tagli): ",len(df_new))
df_new
```
%% Output
Numero misure (iniziali): 4807
Numero misure (dopo i tagli): 3001
/var/folders/ws/ytnwy3cs0hz8z73rvtwydngm0000gn/T/ipykernel_3699/2728226843.py:3: FutureWarning: Index.__or__ operating as a set operation is deprecated, in the future this will be a logical operation matching Series.__or__. Use index.union(other) instead
df_new = df.drop(df[df.t <20 ].index | df[df.t > 80].index)
t angle angle_v angle_a x v a
999 20.00 -1.072855 0.00000 0.000000e+00 -0.025612 0.000000 0.000000e+00
1000 20.02 -1.072855 0.00000 0.000000e+00 -0.025612 0.000000 0.000000e+00
1001 20.04 -1.072855 0.00000 9.817484e-01 -0.025612 0.000000 2.343750e-02
1002 20.06 -1.072855 0.03927 9.817484e-01 -0.025612 0.000937 2.343750e-02
1003 20.08 -1.071284 0.03927 -9.817484e-01 -0.025575 0.000937 -2.343750e-02
... ... ... ... ... ... ... ...
3995 79.92 -8.089607 -0.03927 0.000000e+00 -0.193125 -0.000937 0.000000e+00
3996 79.94 -8.091178 0.00000 1.963497e+00 -0.193162 0.000000 4.687500e-02
3997 79.96 -8.089607 0.03927 9.817484e-01 -0.193125 0.000937 2.343750e-02
3998 79.98 -8.089607 0.03927 3.488529e-13 -0.193125 0.000937 8.326673e-15
3999 80.00 -8.088036 0.03927 -9.817484e-01 -0.193087 0.000937 -2.343750e-02
[3001 rows x 7 columns]
%% Cell type:code id:9a93e1ad tags:
``` python
# grafico nuovo dopo la rimozione delle righe
df_new.plot(x="t",y="x",linestyle="None",marker=".")
```
%% Output
<AxesSubplot:xlabel='t'>
%% Cell type:code id:11174ec4 tags:
``` python
# se volete, e' possibile trasmormare le colonne del dataframe in numpy arrays.
t = df_new.t.to_numpy()
#analogamente
#t = df_new["t"].to_numpy()
x = df_new.x.to_numpy()
t,x
```
%% Output
(array([20. , 20.02, 20.04, ..., 79.96, 79.98, 80. ]),
array([-0.0256125, -0.0256125, -0.0256125, ..., -0.193125 , -0.193125 ,
-0.1930875]))
%% Cell type:code id:03d36daa tags:
``` python
# grafico a partire dai numpy arrays
plt.plot(t,x,linestyle="None",marker=".")
```
%% Output
[<matplotlib.lines.Line2D at 0x15a5f89a0>]
%% Cell type:code id:7d1e0701 tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment