python - Selecting columns in a pandas dataframe -
i have data in different columns don't know how extract save in variable.
index b c 1 2 3 4 2 3 4 5
how select 'b'
, 'c'
, save in df1?
i tried
df1 = df['a':'b'] df1 = df.ix[:, 'a':'b']
none seem work. ideas thanks.
the column names (which strings) cannot sliced in manner tried.
here have couple of options. if know context variables want slice out, can return view of columns passing list __getitem__
syntax (the []'s).
df1 = df[['a','b']]
alternatively, if matters index them numerically , not name (say code should automatically without knowing names of first 2 columns) can instead:
df1 = df.iloc[:,0:2] # remember python not slice inclusive of ending index.
additionally, should familiarize idea of view pandas object vs. copy of object. first of above methods return new copy in memory of desired sub-object (the desired slices).
sometimes, however, there indexing conventions in pandas don't , instead give new variable refers same chunk of memory sub-object or slice in original object. happen second way of indexing, can modify copy()
function regular copy. when happens, changing think sliced object can alter original object. on out this.
df1 = df.iloc[0,0:2].copy() # avoid case changing df1 changes df
Comments
Post a Comment