python - How do I create a DataFrame containing a column where rows are greater than a number? -
i have dataframe these columns:
id int64 key int64 reference object skey float64 sname float64 fkey int64 cname object ints int32
i want create new dataframe containing columns commonname
, ints
ints
greater 10, doing:
df_greater_10 = df[['commonname', df[df.ints >= 1997]]]
i see problem lies expression df[df.ints >= 1997]
i'm returning dataframe - how can column of ints
values greater 10?
you can use 1 of many available indexers. recommend .ix
, because seems faster:
df_greater_10 = df.ix[df.ints >= 1997, ['commonname', 'ints']]
or if need ints
column
df_greater_10 = df.ix[df.ints >= 1997, 'ints']
demo:
in [123]: df = pd.dataframe(np.random.randint(5, 15, (10, 3)), columns=list('abc')) in [124]: df out[124]: b c 0 13 11 14 1 14 10 13 2 7 11 6 3 7 13 12 4 9 9 6 5 7 7 7 6 5 7 8 7 5 11 5 8 9 7 9 9 11 13 7 in [125]: df_greater_10 = df.ix[df.c > 10, ['a','c']] in [126]: df_greater_10 out[126]: c 0 13 14 1 14 13 3 7 12
update: starting pandas 0.20.1 the .ix indexer deprecated, in favor of more strict .iloc , .loc indexers.
so use df.loc[...]
or df.iloc[...]
instead of deprecated df.ix[...]
Comments
Post a Comment