uniXVanXcel
uniXVanXcel

Reputation: 806

How to return the Index when slicing Pandas Dataframe

 df2=   pd.DataFrame(df1.iloc[:, [n for n in random.sample(range(1, 7), 3)]])

returns df1 rows and selected columns but it returns a generic index 0,1,2,3..etc instead of returning the Datetime index of df1 which is what I want to keep. I tried:

df2=df1.copy(deep=True)

df2= pd.DataFrame(data=None, columns=df1.columns, index=df1.index)
df2= df1.iloc[:, [n for n in random.sample(range(1, 7), 3)]]

but it does not work...

Upvotes: 1

Views: 482

Answers (2)

Harshavardhan Ramanna
Harshavardhan Ramanna

Reputation: 738

Try this:

df2 = pd.DataFrame(df1.ix[:,random.sample(range(1,7),3)])

This will give the result you wanted.

df1
Out[130]: 
   one  two
d  NaN  4.0
b  2.0  2.0
c  3.0  3.0
a  1.0  1.0

df1.ix[:,random.sample(range(0,2),2)]
Out[131]: 
   two  one
d  4.0  NaN
b  2.0  2.0
c  3.0  3.0
a  1.0  1.0

This will randomly sample your columns and returns them in df2. This will return all the rows of the randomly sampled columns with index as was in df1.

Edit- As MaxU has suggested, you can simply use:

df2 = df1.ix[:, random.sample(df.columns.tolist(), 3)].copy()

instead of calling the pd.DataFrame() constructor.

Upvotes: 2

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210852

what about slightly different approach?

In [66]: df
Out[66]:
            c1  c2  c3
2016-01-01   4   0   3
2016-01-02   2   3   2
2016-01-03   1   2   3
2016-01-04   3   3   0
2016-01-05   1   0   4
2016-01-06   1   1   1
2016-01-07   2   3   3
2016-01-08   2   2   2
2016-01-09   4   0   0
2016-01-10   1   1   0
2016-01-11   1   3   0
2016-01-12   4   3   1
2016-01-13   0   0   3
2016-01-14   3   1   0
2016-01-15   4   3   1

In [67]: rnd = df.sample(n=6)

In [68]: rnd.index
Out[68]: DatetimeIndex(['2016-01-04', '2016-01-03', '2016-01-12', '2016-01-02', '2016-01-01', '2016-01-13'], dtype='datetime64[ns]', freq=None)

In [69]: rnd
Out[69]:
            c1  c2  c3
2016-01-04   3   3   0
2016-01-03   1   2   3
2016-01-12   4   3   1
2016-01-02   2   3   2
2016-01-01   4   0   3
2016-01-13   0   0   3

Upvotes: 0

Related Questions