GroupBy DataFrame und verschiedene Aggregatfunktionen anwenden, ohne Spaltennamen zu kennen?Python

Python-Programme
Anonymous
 GroupBy DataFrame und verschiedene Aggregatfunktionen anwenden, ohne Spaltennamen zu kennen?

Post by Anonymous »

Ich habe einen Datenrahmen, der eine Schlüsselspalte, einige Wertspalten und einige Zeitstempelspalten hat. Für einige Schlüssel gibt es möglicherweise mehrere Zeilen mit unterschiedlichen Werten in Wert und Zeitstempelspalten. Die Beispieldaten sind unten: < /p>

Code: Select all

data = [['key1', 10, 10, 10, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), 'A'],
['key1', 10, 20, 10, pd.Timestamp('2024-05-11'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-06')],
['key1', 10, 30, 10, pd.Timestamp('2024-05-11'), pd.Timestamp('2024-05-08'), pd.Timestamp('2024-05-12')],
['key2', 10, 10, 10, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09')],
['key2', 12, 10, 10, pd.Timestamp('2024-05-13'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09')],
['key3', 14, 11, 17, pd.Timestamp('2024-06-09'), pd.Timestamp('2024-05-04'), pd.Timestamp('2024-05-01')],
['key4', 10, 10, 12, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-11'), pd.Timestamp('2024-05-29')],
['key5', 10, 10, 10, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-11')],
['key5', 10, 10, 10, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-11')],
['key5', 12, 11, 10, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-11')],
['key5', 10, 11, 10, pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-09'), pd.Timestamp('2024-05-11')]
]
columns = ['Key', 'Value1', 'Value2', 'Value3', 'Timestamp1', 'Timestamp2', 'Timestamp3', 'ID']
sample_df = pd.DataFrame(data, columns=columns)
< /code>
Die tatsächliche Datei enthält Millionen von Zeilen und 100 Spalten. />CountOfrows
Value1
Value2
Timestamp1
Timestamp2
Timestamp3
Id




 Key1 < /td>
 3 < /td>
 10 < /td>
 60 < /td>
 '2024-05-09' < /td>
< /td>
/>'2024-05-09'
'2024-05-12'
'A'


Key2
2
 22 < /td>
 10 < /td>
 '2024-05-09' < /td>
 '2024-05-09' < /td>
 '2024-05-09' < /td>
      />

Key5
4
42
42
'2024-05-09'
 '2024-05-09' < /td>
 '2024-05-11' < /td>
 'b' < /td>
< /tr>
< /tbody>
< /table. Wenn sie anders sind. Die Zeitstempel werden auf der Grundlage von MIN für TimeStamp1, max für TimeStamp2 und TimeStamp3 aggregiert. Für die ID -Spalte werden sie nach der Spalte Wert1 sortiert und ID entspricht dem Max -Wert1 wird < /p>
, da Value3 in allen Fällen eines Schlüssels die gleichen Werte hat, nicht in der Endtabelle enthalten ist.multiple_rows_sample = sample_df.groupby(['Key']).size().reset_index(name='counts')
multiple_rows_sample = multiple_rows_sample[multiple_rows_sample['counts']>1]

mult_val_cols_sample = pd.DataFrame()

for index, row in multiple_rows_sample.iterrows():
joined_slice = sample_df[(sample_df['Key']==row['Key'])]
count_slice = row.to_frame().transpose().reset_index(drop=True)
count_slice['key']=1
diff_cols = cols_having_unique(joined_slice)
diff_cols['key']=1
output_df = pd.merge(count_slice, diff_cols, how='outer')
output_df = output_df.drop('key', axis=1)
mult_val_cols_sample = pd.concat([mult_val_cols_sample, output_df], ignore_index=True)

Die Tabelle mult_val_cols_sample enthält die Schlüsselspalte und nur diese Spalten, in denen sich die Werte für mindestens einen der Schlüssel geändert haben.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post