2

I am learning how to manipulate data using pandas in python. I got the following script:

import pandas as pd

df = pd.read_table( "t.txt" )    #read in the file
df.columns = [x.strip() for x in df.columns]   #strip spaces in headers
df = df.query('TLD == ".biz"')     #select the rows where TLD == ".biz"
df.to_csv('t.txt', sep='\t')  #write the output to a tab-separated file

but the output file has no records, headers only. When I check using

print.df

prior to the selection, the output is:

             TLD  Length                                              Words  \
0       .biz           5                                                ...   
1       .biz           4                                                ...   
2       .biz           5                                                ...   
3       .biz           5                                                ...   
4       .biz           3                                                ...   
5       .biz           3                                                ...   
6       .biz           6                                                ...   

So I know the column TLD has rows with the .biz values. I also tried :

>>> print(df.loc[df['TLD'] == '.biz'])

but the results is

Empty DataFrame

With list of my columns

What am I doing wrong please?

1 Answer 1

5

It seems some whitespaces are there, so need remove them by strip:

print(df.loc[df['TLD'].str.strip() == '.biz'])

df['TLD'] = df['TLD'].str.strip()
df = df.query('TLD == ".biz"')
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Just out of curiosity - how did you recognized white spaces beign there? To my untrained eye it all look good
because you remove whitespaces in columns, so I think in data are too ;)
and maybe simplier for remove whitespaces in columns is df.columns = df.columns.str.strip()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.