pandas列的条件移位

7bsow1i6  于 2023-08-01  发布在  其他
关注(0)|答案(2)|浏览(98)

如何正确地有条件地移动pandas DataFrame中的列?我想移动特定列中对应的“i”值大于6的所有值。我目前使用的代码没有产生预期的结果。
下面是我的代码:

import pandas as pd
import numpy as np

# Creating data for 'i' and 'price' columns
n = 10  # Number of entries
i_values = list(range(1, n+1))
price_values = [10.99, 19.99, 5.99, 8.49, 12.99, 15.99, 9.99, 14.99, 6.99, 11.99]

# Creating DataFrame
data = {'i': i_values,
        'price': price_values}
df = pd.DataFrame(data)

df['price_new'] = df.loc[df.i>6, 'price'].shift(-3)

字符串
预期输出应为:

n = 10  # Number of entries
i_values = list(range(1, n+1))
price_values = [10.99, 19.99, 5.99, 8.49, 12.99, 15.99, 9.99, 14.99, 6.99, 11.99]
new_price_values = [np.NaN, np.NaN, np.NaN, 9.99, 14.99, 6.99, 11.99, np.NaN, np.NaN, np.NaN]

# Creating DataFrame
data = {'i': i_values,
        'price': price_values,
        'new_price': new_price_values}
df = pd.DataFrame(data)

u7up0aaq

u7up0aaq1#

一个可能的解决方案是过滤i > 6的值,分配给新列并将其移位:

df['new_price_2'] = np.where(df.i > 6, df['price'], np.nan)
df['new_price_2'] = df['new_price_2'].shift(-3)

print(df)

字符串
图纸:

i  price  new_price  new_price_2
0   1  10.99        NaN          NaN
1   2  19.99        NaN          NaN
2   3   5.99        NaN          NaN
3   4   8.49       9.99         9.99
4   5  12.99      14.99        14.99
5   6  15.99       6.99         6.99
6   7   9.99      11.99        11.99
7   8  14.99        NaN          NaN
8   9   6.99        NaN          NaN
9  10  11.99        NaN          NaN

osh3o9ms

osh3o9ms2#

要在pandas DataFrame中正确地有条件地移动列,可以使用np.where函数沿着布尔索引来实现所需的结果。以下是您的操作方法:

import pandas as pd
import numpy as np

# Creating data for 'i' and 'price' columns
n = 10  # Number of entries
i_values = list(range(1, n+1))
price_values = [10.99, 19.99, 5.99, 8.49, 12.99, 15.99, 9.99, 14.99, 6.99, 11.99]

# Creating DataFrame
data = {'i': i_values,
        'price': price_values}
df = pd.DataFrame(data)

# Conditionally shift the 'price' column where 'i' > 6
df['new_price'] = np.where(df['i'] > 6, df['price'].shift(-3), np.nan)

print(df)

字符串
输出量:

i  price  new_price
0   1  10.99        NaN
1   2  19.99        NaN
2   3   5.99        NaN
3   4   8.49       9.99
4   5  12.99      14.99
5   6  15.99       6.99
6   7   9.99      11.99
7   8  14.99        NaN
8   9   6.99        NaN
9  10  11.99        NaN

相关问题