python - Create dataframe from specific column -
i trying create dataframe in pandas ab column in csv file. (ab 27th column).
i using line:
df = pd.read_csv(filename, error_bad_lines = false, usecols = [27]) ... resulting in error:
valueerror: usecols not match names. i'm new pandas, point out i'm doing wrong me?
here small demo:
csv file (without header, i.e. there no column names):
1,2,3,4,5,6,7,8,9,10 11,12,13,14,15,16,17,18,19,20 we going read 8-th column:
in [1]: fn = r'd:\temp\.data\1.csv' in [2]: df = pd.read_csv(fn, header=none, usecols=[7], names=['col8']) in [3]: df out[3]: col8 0 8 1 18 ps pay attention @ header=none, usecols=[7], names=['col8']
if don't use header=none , names parameters, first row used header:
in [6]: df = pd.read_csv(fn, usecols=[7]) in [7]: df out[7]: 8 0 18 in [8]: df.columns out[8]: index(['8'], dtype='object') and if want read last 10-th column:
in [9]: df = pd.read_csv(fn, usecols=[10]) ... skipped ... valueerror: usecols not match names. because pandas counts columns starting 0, have way:
in [12]: df = pd.read_csv(fn, usecols=[9], names=['col10']) in [13]: df out[13]: col10 0 10 1 20
Comments
Post a Comment