Reputation: 767
I've created a loop to get a list of rows to drop.
cols = []
for i in range(len(df)):
if i != len(df)-1:
if (df['Player'][i] == df['Player'][i+1]):
cols.append(i+1)
Now I want to go through cols and drop each row by its number. I've tried using the following loop to drop these rows but it's not working.
for col in cols:
df.drop([[col]])
Upvotes: 0
Views: 1832
Reputation: 3130
It looks like you are trying to get rid of rows preceded by a duplicate value of Player
. With pandas
, always try to work with vectorization: df.Player == df.Player.shift()
checks whether each entry in Player
is equal to the last, so a one-line solution is
df[df.Player != df.Player.shift()]
Upvotes: 0
Reputation: 1102
if you just want to delete rows with duplicated value in col 'Player', you can:
df.drop_duplicates(subset=['Player'],inplace=True)
Upvotes: 0
Reputation: 2006
For dropping rows:
df.drop(rows,inplace=True)
For dropping columns:
df.drop(cols,axis=1,inplace=True)
Test:
df = pd.DataFrame({'A':[1,2,3,4,5],'B':[1,2,3,4,5]})
print('DataFrame with initialised :\n',df,'\n')
df.drop([2,4],inplace=True)
print('DataFrame after dropping 2 rows:\n',df,'\n')
df.drop(['B'],axis=1,inplace=True)
print('DataFrame after dropping 1 column:\n',df,'\n')
Output:
DataFrame with initialised :
A B
0 1 1
1 2 2
2 3 3
3 4 4
4 5 5
DataFrame after dropping 2 rows:
A B
0 1 1
1 2 2
3 4 4
DataFrame after dropping 1 column:
A
0 1
1 2
3 4
Upvotes: 1