Aby potwierdzić, że rozumiem, co robią Pandy df.groupby()
i df.reset_index()
, próbowałem wykonać obieg z ramki danych do zgrupowanej wersji tych samych danych iz powrotem. Po w obie kolumny i wiersze musiały być ponownie sortowane, bo groupby()
wpływa na kolejność wierszy i reset_index()
wpływa na kolejność kolumn, ale po dwóch szybkich manewrów, aby umieścić kolumny i indeks z powrotem w porządku, dataframes wyglądają identycznie:W jaki sposób Pandas DataFrames może wyglądać identycznie, ale kończy się niepowodzeniem()?
- Ta sama lista nazw kolumn.
- Te same dtypes dla każdej kolumny.
- Odpowiednie wartości indeksu są ściśle równe.
- Odpowiednie wartości danych są ściśle równe.
Jednak po pomyślnym zakończeniu wszystkich tych testów df1.equals(df5)
zwraca zdumiewającą wartość False
.
Jaka jest różnica między tymi ramkami danych: equals()
ujawniając, że jeszcze nie wymyśliłem, jak sprawdzić samemu? Kod
Test:
csv_text = """\
Title,Year,Director
North by Northwest,1959,Alfred Hitchcock
Notorious,1946,Alfred Hitchcock
The Philadelphia Story,1940,George Cukor
To Catch a Thief,1955,Alfred Hitchcock
His Girl Friday,1940,Howard Hawks
"""
import pandas as pd
df1 = pd.read_csv('sample.csv')
df1.columns = map(str.lower, df1.columns)
print(df1)
df2 = df1.groupby(['director', df1.index]).first()
df3 = df2.reset_index('director')
df4 = df3[['title', 'year', 'director']]
df5 = df4.sort_index()
print(df5)
print()
print(repr(df1.columns))
print(repr(df5.columns))
print()
print(df1.dtypes)
print(df5.dtypes)
print()
print(df1 == df5)
print()
print(df1.index == df5.index)
print()
print(df1.equals(df5))
Wyjście że otrzymam gdy uruchamiam skrypt jest:
title year director
0 North by Northwest 1959 Alfred Hitchcock
1 Notorious 1946 Alfred Hitchcock
2 The Philadelphia Story 1940 George Cukor
3 To Catch a Thief 1955 Alfred Hitchcock
4 His Girl Friday 1940 Howard Hawks
title year director
0 North by Northwest 1959 Alfred Hitchcock
1 Notorious 1946 Alfred Hitchcock
2 The Philadelphia Story 1940 George Cukor
3 To Catch a Thief 1955 Alfred Hitchcock
4 His Girl Friday 1940 Howard Hawks
Index(['title', 'year', 'director'], dtype='object')
Index(['title', 'year', 'director'], dtype='object')
title object
year int64
director object
dtype: object
title object
year int64
director object
dtype: object
title year director
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
[ True True True True True]
False
dzięki za pomoc!
Innym sposobem sprawdzenia jest użycie 'pandas.util.testing.assert_frame_equal'. To może dać ci raport o tym, co myśli Panda jest inna. – jiffyclub
Dobry pomysł! Wypróbowałem to. Nie wywołuje żadnego wyjątku, jeśli jego argumenty to "assert_frame_equal (df1, df5)" jako ostatnia linia w powyższym skrypcie. Wydaje się więc, że są one równe, chociaż '.equals()' nie działa? –