"/var/folders/ws/ytnwy3cs0hz8z73rvtwydngm0000gn/T/ipykernel_40871/1233098160.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\n",
Esempio di lettura di un file di test dell'esperimento "Carrello". 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:7d6638fa tags:
``` python
#plots will be shown inline
%matplotlibinline
importnumpyasnp
importmatplotlib.pyplotasplt
importmatplotlib.mlabasmlab
fromscipyimportstats
importpandasaspd
```
%% Cell type:code id:28e10b83 tags:
``` python
# sostituire la virgola con il punto
reading_file=open("dati_carrello.txt","r")
new_file_content=""
forlineinreading_file:
stripped_line=line.strip()
new_line=stripped_line.replace(",",".")
new_file_content+=new_line+"\n"
reading_file.close()
writing_file=open("dati_carrello.txt","w")
writing_file.write(new_file_content)
writing_file.close()
```
%% Cell type:code id:913eb5c9 tags:
``` python
df=pd.read_csv('dati_carrello.txt',header=1,sep='\t')#il separatore in questo caso era un "tab" (\t)
df
```
%% Output
Tempo ( s ) Tempo trascorso ( s )
0 0.0508 0.000685
1 0.1009 0.000688
2 0.1510 0.000685
3 0.2011 0.000685
4 0.2512 0.000685
.. ... ...
114 5.7723 0.010810
115 5.8222 0.010661
116 5.8722 0.010560
117 5.9222 0.010463
118 5.9722 0.010342
[119 rows x 2 columns]
%% Cell type:code id:f5f6e2b1 tags:
``` python
#rinominare le colonne
df=df.rename(columns={"Tempo ( s )":"t","Tempo trascorso ( s )":"DeltaT"})
#selezionare un intervallo di tempo (rimuovere alcuni righe del dataframe)
print ("Numero misure (iniziali): ",len(df))
df_new=df.drop(df[df.t<2].index|df[df.t>5].index)
index_to_be_drop=df[(df.t<2)|(df.t>5)].index
#df_new = df.drop(df[df.t <2 ].index | df[df.t > 5].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): 119
Numero misure (dopo i tagli): 60
/var/folders/ws/ytnwy3cs0hz8z73rvtwydngm0000gn/T/ipykernel_40871/1233098160.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