I have a list where every element is Dataframe itself. And theses Dfs have duplicate date time index. I want to remove every duplicate index for every Df in that list.
list_dfs = [df_1, df_2, df_3, df_4]
dtype='datetime64[ns]' #Index of all Dfs in list_dfs
I am Using this list comprehension code. It is removing the duplicate indices but also with columns. At end i ended up only with the indices.
[df.index.drop_duplicates(keep='last') for df in list_dfs]
Any idea how one can achieve it?
Use Index.duplicated
with filtering by boolean indexing
and ~
for invering boolean mask:
df = pd.DataFrame({
'A':list('abcdef'),
'F':list('aaabbb')
}).set_index('F')
df1 = pd.DataFrame({
'A':list('tyuio'),
'F':list('rrffv')
}).set_index('F')
list_dfs = [df, df1]
L = [df[~df.index.duplicated(keep='last')] for df in list_dfs]
print (L)
[ A
F
a c
b f, A
F
r y
f i
v o]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.