python - read csv files pandas slice arrays -
i'm python newbie , having trouble reading csv pandas , working it. here bit of csv file:
a b 1 56 2 76 3 23 4 45 5 54 6 65 7 22 and python code:
import numpy np import pandas pd math import exp math import sqrt g = pd.dataframe.from_csv('test.csv') = g.iloc[2:4,1] print(a) i following error:
indexerror: index 1 out of bounds axis 0 size 1 i've tried:
a = g.iloc[2:4,'b'] and many other permutations defining columns , rows.
also when print g, following:
b 2015-05-01 56 2015-05-02 76 2015-05-03 23 2015-05-04 45 2015-05-05 54 2015-05-06 65 2015-05-07 22 i can't understand why , b not aligned.
i'm using example, in general i'd read in large csv files , perform operations on aspects of matrix.
any appreciated.
firstly dataframe.from_csv whilst still supported, it's better use top level read_csv instead supports more functionality.
so this:
a = g.iloc[2:4,1] is wrong syntax, want:
a = g.iloc[2:4]['a'] secondly, default dataframe.from_csv uses first column index why column 'a' index, if passed index_col=none desired result:
in [6]: pd.dataframe.from_csv(file_path) out[6]: b 1 56 2 76 3 23 4 45 5 54 6 65 7 22 in [7]: pd.dataframe.from_csv(file_path, index_col=none) out[7]: b 0 1 56 1 2 76 2 3 23 3 4 45 4 5 54 5 6 65 6 7 22 correct syntax:
in [9]: df.iloc[2:4]['a'] out[9]: 2 3 3 4 name: a, dtype: int64 additionally read_csv default index_col none problem alignment not have happened if had used read_csv.
please check docs on indexing , selecting.
edit
as @jeff suggested , agree jeff, kind of selection ix typical selection method it's behaviour differs iloc in include end row selection unlike iloc:
in [10]: df.ix[2:4,'a'] out[10]: 2 3 3 4 4 5 name: a, dtype: int64 so don't know wanted row selection-wise aware of different semantics.
update
note .ix deprecated in future, can achieve same result using .loc:
in [202]: df.loc[2:4,'a'] out[202]: 2 3 3 4 4 5 name: a, dtype: int64
Comments
Post a Comment