-1

enter image description here

Can anyone tell me how can I remove all 'A's and other data like this from the data frame? and I also want to remove XXXX rows from the data frame.

prabs
  • 81
  • 1
  • 7
  • https://meta.stackoverflow.com/q/303812/11301900. What have you tried? Have you done any research? This looks like a duplicate of https://stackoverflow.com/q/19937362/11301900. – AMC Jan 16 '20 at 22:40

2 Answers2

3

Use Series.str.len with Series.ne to performance a boolean indexing

if you want to delete the column where name is A :

df[df['name'].ne('A') & df['year'].ne('XXXX'))]

to detect when lenght of string in column name is greater than one.

df[df['name'].str.len().gt(1) & df['year'].ne('XXXX')]
ansev
  • 30,322
  • 5
  • 17
  • 31
1

In order to remove all the lines where in column name you have 1-character long string just do:

df = df.drop(df.index[df["name"].str.len().eq(1)], axis=0)

Similarly for the XXXX rows:

df = df.drop(df.index[df["year"].eq("XXXX")], axis=0)

And combined:

df = df.drop(df.index[df["name"].str.len().eq(1) | df["year"].eq("XXXX")],axis=0)
smci
  • 32,567
  • 20
  • 113
  • 146
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34