简体   繁体   中英

How to Look-up values in Pandas dataframe

I have a dataframe like this.

Date        Ticker      Price
2019-03-21    AAPL        100
2019-03-21    GOOG        101
2019-03-21    IBM         102
2019-03-25    AAPL         90
2019-03-25    GOOG         91
2019-03-25    IBM          92
2019-03-27    AAPL        110
2019-03-27    GOOG        111
2019-03-27    IBM         112

I am trying to add a column called 'LastPrice' which finds the Ticker's last date price. Dates are not consecutive. Thanks.

Date        Ticker      Price      LastPrice
2019-03-21    AAPL        100
2019-03-21    GOOG        101
2019-03-21    IBM         102
2019-03-25    AAPL         90            100
2019-03-25    GOOG         91            101
2019-03-25    IBM          92            102
2019-03-27    AAPL        110             90
2019-03-27    GOOG        111             91
2019-03-27    IBM         112             92

Suppose your data is ordered by date, you can use groupby and shift.

df['LastPrice'] = (
    df.groupby('Ticker')
    .apply(lambda x: x.Price.shift())
    .reset_index(0, drop=True)
)

    Date        Ticker  Price   LastPrice
0   2019-03-21  AAPL    100     NaN
1   2019-03-21  GOOG    101     NaN
2   2019-03-21  IBM     102     NaN
3   2019-03-25  AAPL    90      100.0
4   2019-03-25  GOOG    91      101.0
5   2019-03-25  IBM     92      102.0
6   2019-03-27  AAPL    110     90.0
7   2019-03-27  GOOG    111     91.0
8   2019-03-27  IBM     112     92.0

You can lookup values using many methods, here is one of the easier ways.

  1. Look up the price for AAPL. df["Ticker"]=="AAPL" will return an array of True/False values. True when df["Ticker"] contains "AAPL" . df.loc will locate in the dataframe, where the True value in the df["Ticker"]=="AAPL" array corresponds with the row in the dataframe. Thats why you only see rows where df["Ticker"]=="AAPL" .
df # your df

df_AAPL = df.loc[df["Ticker"]=="AAPL"]
  1. To get the price, you can use df.loc to locate the price column.
df_AAPL_price = df_AAPL.loc[:,"Price"]
  1. To get the price when "Ticker" == "AAPL" on a certain date (assuming your date is in str), you can use a lambda function and apply it across the columns in the dataframe, hence axis = 1 . This function takes in values in the rows and returns True if row["Date"] == "2019-03-27" and row["Ticker"] == "AAPL" else False . Same concept as in point 1, df.loc is used to locate where in the dataframe that True occurs in the array. You can think of it as dataframe = [1,2,3], array = [True, False, True], and match them up, then only take the value if it is True in the array. So, in this case it would be only "1" and "3".
df_new = df.loc[df.apply(lambda row:True if row["Date"] == "2019-03-27" and row["Ticker"] == "AAPL" else False ,axis=1)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM