Ignoring NaNs with str.contains

I want to find rows that contain a string, like so: DF[DF.col.str.contains(“foo”)] However, this fails because some elements are NaN: ValueError: cannot index with vector containing NA / NaN values So I resort to the obfuscated DF[DF.col.notnull()][DF.col.dropna().str.contains(“foo”)] Is there a better way? 6 Answers 6

How to load a tsv file into a Pandas DataFrame?

I’m new to python and pandas. I’m trying to get a tsv file loaded into a pandas DataFrame. This is what I’m trying and the error I’m getting: >>> df1 = DataFrame(csv.reader(open(‘c:/~/trainSetRel3.txt’), delimiter=”\t”)) Traceback (most recent call last): File “<pyshell#28>”, line 1, in <module> df1 = DataFrame(csv.reader(open(‘c:/~/trainSetRel3.txt’), delimiter=”\t”)) File “C:\Python27\lib\site-packages\pandas\core\frame.py”, line 318, in __init__ raise … Read more

datetime dtypes in pandas read_csv

I’m reading in a csv file with multiple datetime columns. I’d need to set the data types upon reading in the file, but datetimes appear to be a problem. For instance: headers = [‘col1’, ‘col2’, ‘col3’, ‘col4’] dtypes = [‘datetime’, ‘datetime’, ‘str’, ‘float’] pd.read_csv(file, sep=’\t’, header=None, names=headers, dtype=dtypes) When run gives a error: TypeError: data … Read more

Can pandas automatically read dates from a CSV file?

Today I was positively surprised by the fact that while reading data from a data file (for example) pandas is able to recognize types of values: df = pandas.read_csv(‘test.dat’, delimiter=r”\s+”, names=[‘col1′,’col2′,’col3’]) For example it can be checked in this way: for i, r in df.iterrows(): print type(r[‘col1’]), type(r[‘col2’]), type(r[‘col3’]) In particular integer, floats and strings … Read more

Pandas read_csv from url

I’m trying to read a csv-file from given URL, using Python 3.x: import pandas as pd import requests url = “https://github.com/cs109/2014_data/blob/master/countries.csv” s = requests.get(url).content c = pd.read_csv(s) I have the following error “Expected file path name or file-like object, got <class ‘bytes’> type” How can I fix this? I’m using Python 3.4 6 Answers 6