How to read categorical columns with pandas’ read_csv?

In version 0.19.0 you can use parameter dtype=”category” in read_csv: data=”col1,col2,col3\na,b,1\na,b,2\nc,d,3″ df = pd.read_csv(pd.compat.StringIO(data), dtype=”category”) print (df) col1 col2 col3 0 a b 1 1 a b 2 2 c d 3 print (df.dtypes) col1 category col2 category col3 category dtype: object If want specify column for category use dtype with dictionary: df = pd.read_csv(pd.compat.StringIO(data), … Read more

How to read-write into/from text file with comma separated values

Step 1: Don’t do this: while(!file.eof()) { getline(file,line); numlines++; } numline–; The EOF is not true until you try and read past it. The standard pattern is: while(getline(file,line)) { ++numline; } Also note that std::getline() can optionally take a third parameter. This is the character to break on. By default this is the line terminator … Read more

Deleting rows with Python in a CSV file

You are very close; currently you compare the row[2] with integer 0, make the comparison with the string “0”. When you read the data from a file, it is a string and not an integer, so that is why your integer check fails currently: row[2]!=”0″: Also, you can use the with keyword to make the … Read more

Convert large csv to hdf5

Use append=True in the call to to_hdf: import numpy as np import pandas as pd filename=”/tmp/test.h5″ df = pd.DataFrame(np.arange(10).reshape((5,2)), columns=[‘A’, ‘B’]) print(df) # A B # 0 0 1 # 1 2 3 # 2 4 5 # 3 6 7 # 4 8 9 # Save to HDF5 df.to_hdf(filename, ‘data’, mode=”w”, format=”table”) del df … Read more

Python csv without header

You can still use your line, if you declare the headers yourself, since you know it: with open(‘data.csv’) as f: cf = csv.DictReader(f, fieldnames=[‘city’]) for row in cf: print row[‘city’] For more information check csv.DictReader info in the docs. Another option is to just use positional indexing, since you know there’s only one column: with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)