Reputation: 3659
I have a panda dataframe df with the contents below;
Date Factor Expiry Grade
0 12/31/1991 2.138766 3/30/1992 -3.33%
1 10/29/1992 2.031381 2/8/1993 -1.06%
2 5/20/1993 2.075670 6/4/1993 -6.38%
3 10/11/1994 1.441644 11/22/1994 -7.80%
4 1/11/1995 1.669600 1/20/1995 -7.39%
5 5/15/1995 1.655237 8/8/1995 -8.68%
6 10/17/1996 0.942000 10/22/1996 -7.39%
7 2/19/1998 0.838838 5/26/1998 13.19%
8 7/9/1998 1.303637 8/28/1998 -6.73%
9 12/29/1998 1.517232 1/21/1999 -11.03%
10 4/26/1999 1.613346 5/24/1999 -7.55%
11 7/8/1999 2.136339 9/23/1999 5.43%
12 3/22/2000 5.097782 3/29/2000 -6.44%
I would like to extract out Date
and Expiry
column values corresponding to the rows with Grade <=-8%.
The desirable output will be a list of list of strings like this;
output_dates = [ ['5/15/1995', '8/8/1995'], ['12/29/1998', '1/21/1999'] ]
My current solution is now able to extract out only Date
.
out = df.loc[df['Grade'].str.rstrip('%').astype(float).le(-8), 'Date']
which gives ['5/15/1995', '12/29/1998']
. However, this is not good enough as it can only retrieve one column value.
I am using python v3.6
Upvotes: 1
Views: 38
Reputation: 862611
You can define columns in list:
cols = ['Expiry', 'Date']
print (df.loc[df['Grade'].str.rstrip('%').astype(float).le(-8), cols])
Expiry Date
5 8/8/1995 5/15/1995
9 1/21/1999 12/29/1998
If need output list
s is necessary first convert to numpy array and then to list
:
out = df.loc[df['Grade'].str.rstrip('%').astype(float).le(-8), cols].values.tolist()
print (out)
[['8/8/1995', '5/15/1995'], ['1/21/1999', '12/29/1998']]
Upvotes: 1