I have a dataframe like this:
JavaScript
x
18
18
1
>>> df = pd.DataFrame({
2
'Date': [
3
pd.to_datetime("2022-07-01"),
4
pd.to_datetime("2020-07-02"),
5
pd.to_datetime("2020-07-03"),
6
],
7
"Price": [
8
{24.9, 23.0, 22.5, 23.5},
9
{24.9, 25.0, 26.5, 23.7},
10
{25.2, 24.5, 23.6, 23.8},
11
]})
12
13
>>> df
14
Date Price
15
0 2022-07-01 {24.9, 23.5, 22.5, 23.0}
16
1 2020-07-02 {24.9, 25.0, 26.5, 23.7}
17
2 2020-07-03 {24.5, 25.2, 23.8, 23.6}
18
I want to add a new column ‘intersec’ and get the intersection of the Price column and its shift value. But when I use
JavaScript
1
2
1
df['intersec'] = df.price[1:]&df.price.shift()[1:]
2
It doesn’t work, I get the following error:
JavaScript
1
2
1
TypeError: unsupported operand type(s) for &: 'set' and 'bool'
2
what should I do? My expected result is:
JavaScript
1
6
1
>>> df
2
Date Price intersec
3
0 2022/7/1 {24.9, 23.5, 22.5, 23.0} NaN
4
1 2020/7/2 {24.9, 25.0, 26.5, 23.7} 24.9
5
2 2020/7/3 {24.5, 25.2, 23.8, 23.6} NaN
6
Advertisement
Answer
shift the price
JavaScript
1
2
1
df["shifted_price"] = df.Price.shift()
2
find intersect
JavaScript
1
2
1
df["intersec"] = df[1:].apply(lambda x: list(set(x["Price"]) & set(x["shifted_price"])), axis=1)
2
sample output
JavaScript
1
5
1
Date Price shifted_price intersec
2
0 2022-07-01 {24.9, 23.5, 22.5, 23.0} NaN NaN
3
1 2020-07-02 {24.9, 25.0, 26.5, 23.7} {24.9, 23.5, 22.5, 23.0} [24.9]
4
2 2020-07-03 {24.5, 25.2, 23.8, 23.6} {24.9, 25.0, 26.5, 23.7} []
5