Désolé d'avance pour le long tableau de données. Je ne connais pas de manière plus succincte de construire le dataframe que j'ai ci-dessous.
J'ai un DataFrame pandas:
data = {'ID': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'Cycle': [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4],
'Repetition': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2'],
'Region': ['x', 'x','x','x','x','x','x','x', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'x','x','x','x','x','x','x','x', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y'],
'Intensity': [34, 89, 34, 45, 34, 56, 78, 65, 45, 45, 34, 56, 34, 56, 56, 66, 56, 78, 23, 45, 42, 56, 86, 5, 33, 44, 78, 89, 34, 42, 34, 66]}
data_df= pd.DataFrame(data)
Je voudrais ajouter une colonne qui calcule l'intensité moyenne lorsque Cycle == 1
pour chaque ID (A et B) et chaque région (x et y) et laisse les valeurs NaN dans toutes les autres lignes. Le dataframe résultant renverrait:
wanted_data = {'ID': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'Cycle': [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4],
'Repetition': ['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2'],
'Region': ['x', 'x','x','x','x','x','x','x', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'x','x','x','x','x','x','x','x', 'y', 'y', 'y', 'y', 'y', 'y', 'y', 'y'],
'Intensity': [34, 89, 34, 45, 34, 56, 78, 65, 45, 45, 34, 56, 34, 56, 56, 66, 56, 78, 23, 45, 42, 56, 86, 5, 33, 44, 78, 89, 34, 42, 34, 66],
'Mean Cycle1 Intensity': [39.5, '', '', '', 34, '', '', '', '', '', '', '', '', '', '', '', 44.5, '', '', '', 38, '', '', '', '', '', '', '', '', '', '', ''] }
wanted_data_df= pd.DataFrame(wanted_data)
J'ai essayé d'ajouter une fonction:
def meanC1(df):
for i in df['ID'] and j in df['Region']:
if df['Cycle'] == 1:
df['Mean Cycle1 Intensity'] = df['Intensity'].mean()
Mais cela revient,
ValueError: la valeur de vérité d'une série est ambiguë. Utilisez a.empty, a.bool (), a.item (), a.any () ou a.all ()
Utilisez Series.ne
pour créer un masque booléen m
, puis utilisez Series.mask
pour masquer la Intensity
colonne m
, utilisez ensuite Series.groupby
pour regrouper la colonne masquée sur ID
et Repetition
et en transform
utilisant mean
, enfin à nouveau utiliser Series.mask
pour masquer le résultat transformé:
# Note: Here df refers to `data_df`
m = df['Cycle'].ne(1)
df['Mean Cycle1 Intensity'] = (
df['Intensity'].mask(m)
.groupby([df['ID'], df['Repetition']]).transform('mean').mask(m)
)
Résultat:
ID Cycle Repetition Region Intensity Mean Cycle1 Intensity
0 A 1 1 x 34 39.5
1 A 2 1 x 89 NaN
2 A 3 1 x 34 NaN
3 A 4 1 x 45 NaN
4 B 1 1 x 34 34.0
5 B 2 1 x 56 NaN
6 B 3 1 x 78 NaN
7 B 4 1 x 65 NaN
8 A 1 1 y 45 39.5
9 A 2 1 y 45 NaN
10 A 3 1 y 34 NaN
11 A 4 1 y 56 NaN
12 B 1 1 y 34 34.0
13 B 2 1 y 56 NaN
14 B 3 1 y 56 NaN
15 B 4 1 y 66 NaN
16 A 1 2 x 56 44.5
17 A 2 2 x 78 NaN
18 A 3 2 x 23 NaN
19 A 4 2 x 45 NaN
20 B 1 2 x 42 38.0
21 B 2 2 x 56 NaN
22 B 3 2 x 86 NaN
23 B 4 2 x 5 NaN
24 A 1 2 y 33 44.5
25 A 2 2 y 44 NaN
26 A 3 2 y 78 NaN
27 A 4 2 y 89 NaN
28 B 1 2 y 34 38.0
29 B 2 2 y 42 NaN
30 B 3 2 y 34 NaN
31 B 4 2 y 66 NaN
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots