pythonpandasdataframeempty-list

Unwanted empty dataframe using pandas in python


I am trying to learn python now for 2 full weeks. I am encountering a problem witch I can not solve. It's seems very easy to do when I see examples on the internet. Despite that I can't get it done.

I am trying to get information from the wind speed (column: 5) and by using it's wind station number (column:0).

I am already capable of reading the text file and print it:

import pandas as pd

## data
os.chdir('C:\\Users\\########\\sensoren\\data')
os.getcwd()
wind = pd.read_csv('weerdata_299_235_242_251_267_277.txt', usecols= [0 ,5 ],skiprows = 33, names = ['STN','FF'])
print(wind)

giving the following results:
       STN     FF
0      229     60
1      229     60
2      229     60
3      229     60
4      229     30
   ...    ...
62779  277     50
62780  277     50
62781  277     50
62782  277     60
62783  277     50

Now I want to get only the data of the rows that contain STN:277 . Where I used this code for:

wind_277 = wind[wind['STN'] == '277']
print(wind_277)

Which gives this result:

Empty DataFrame
Columns: [STN, FF]
Index: []

I do not understand why this DataFrame is empty. Can anyone help me with this?


Solution

  • My guess is that it is because the column STN are integers. Check the data types by. print(wind.dtypes)

    if it is integers then remove the string note on 277.

    wind_277 = wind[wind['STN'] == 277]
    

    But maybe it is a string such as '277 ', that has trailing spaces. In this situation you need to remove the spaces.

    wind['STN'] = wind['STN'].str.strip()