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

fix problem for df drop

parent 8fe74ecf
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:be9885c3 tags:
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
%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:28e10b83 tags:
``` python
# sostituire la virgola con il punto
reading_file = open("dati_carrello.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_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"})
df
```
%% Output
t DeltaT
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:033725f4 tags:
``` python
# fare un grafico direttamente dal dataframe
df.plot(x="t",y="DeltaT",linestyle="None",marker=".")
```
%% Output
<AxesSubplot:xlabel='t'>
%% Cell type:code id:36271ccd 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 <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
df_new = df.drop(df[df.t <2 ].index | df[df.t > 5].index)
t DeltaT
39 2.0047 0.000685
40 2.0548 0.000725
41 2.1050 0.000793
42 2.1551 0.000875
43 2.2053 0.000947
44 2.2555 0.001020
45 2.3057 0.001095
46 2.3558 0.001176
47 2.4060 0.001252
48 2.4562 0.001332
49 2.5064 0.001421
50 2.5566 0.001512
51 2.6068 0.001608
52 2.6570 0.001709
53 2.7072 0.001811
54 2.7574 0.001917
55 2.8076 0.002026
56 2.8578 0.002138
57 2.9080 0.002254
58 2.9582 0.002372
59 3.0085 0.002494
60 3.0587 0.002619
61 3.1089 0.002749
62 3.1591 0.002880
63 3.2094 0.003017
64 3.2596 0.003156
65 3.3099 0.003298
66 3.3601 0.003443
67 3.4104 0.003591
68 3.4606 0.003741
69 3.5109 0.003895
70 3.5611 0.004052
71 3.6114 0.004211
72 3.6616 0.004374
73 3.7119 0.004541
74 3.7622 0.004709
75 3.8124 0.004880
76 3.8627 0.005054
77 3.9130 0.005232
78 3.9633 0.005411
79 4.0136 0.005595
80 4.0639 0.005782
81 4.1141 0.005971
82 4.1644 0.006162
83 4.2147 0.006357
84 4.2650 0.006555
85 4.3153 0.006755
86 4.3656 0.006958
87 4.4159 0.007162
88 4.4662 0.007371
89 4.5165 0.007580
90 4.5669 0.007794
91 4.6172 0.008008
92 4.6675 0.008227
93 4.7178 0.008447
94 4.7681 0.008671
95 4.8185 0.008896
96 4.8688 0.009124
97 4.9191 0.009354
98 4.9695 0.009586
%% Cell type:code id:767eb950 tags:
``` python
# grafico nuovo dopo la rimozione delle righe
df_new.plot(x="t",y="DeltaT",linestyle="None",marker=".")
```
%% Output
<AxesSubplot:xlabel='t'>
%% Cell type:code id:915d60b5 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()
DeltaT = df_new.DeltaT.to_numpy()
t,DeltaT
```
%% Output
(array([2.0047, 2.0548, 2.105 , 2.1551, 2.2053, 2.2555, 2.3057, 2.3558,
2.406 , 2.4562, 2.5064, 2.5566, 2.6068, 2.657 , 2.7072, 2.7574,
2.8076, 2.8578, 2.908 , 2.9582, 3.0085, 3.0587, 3.1089, 3.1591,
3.2094, 3.2596, 3.3099, 3.3601, 3.4104, 3.4606, 3.5109, 3.5611,
3.6114, 3.6616, 3.7119, 3.7622, 3.8124, 3.8627, 3.913 , 3.9633,
4.0136, 4.0639, 4.1141, 4.1644, 4.2147, 4.265 , 4.3153, 4.3656,
4.4159, 4.4662, 4.5165, 4.5669, 4.6172, 4.6675, 4.7178, 4.7681,
4.8185, 4.8688, 4.9191, 4.9695]),
array([0.000685, 0.000725, 0.000793, 0.000875, 0.000947, 0.00102 ,
0.001095, 0.001176, 0.001252, 0.001332, 0.001421, 0.001512,
0.001608, 0.001709, 0.001811, 0.001917, 0.002026, 0.002138,
0.002254, 0.002372, 0.002494, 0.002619, 0.002749, 0.00288 ,
0.003017, 0.003156, 0.003298, 0.003443, 0.003591, 0.003741,
0.003895, 0.004052, 0.004211, 0.004374, 0.004541, 0.004709,
0.00488 , 0.005054, 0.005232, 0.005411, 0.005595, 0.005782,
0.005971, 0.006162, 0.006357, 0.006555, 0.006755, 0.006958,
0.007162, 0.007371, 0.00758 , 0.007794, 0.008008, 0.008227,
0.008447, 0.008671, 0.008896, 0.009124, 0.009354, 0.009586]))
%% Cell type:code id:5d3d8d6a tags:
``` python
# grafico a partire dai numpy arrays
plt.plot(t,DeltaT,linestyle="None",marker=".")
```
%% Output
[<matplotlib.lines.Line2D at 0x165d00fd0>]
[<matplotlib.lines.Line2D at 0x164520ee0>]
%% Cell type:code id:e217e3b2 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