当熊猫满足条件时,从下面一行返回值

时间:2020-08-02 21:02:58

标签: python pandas

我是Python的新手,我想找出一种方法,在满足某些条件时从pandas DataFrame中选择某些“单元格”。举个例子,假设您拥有这种类型的数据:

Number Country
1      Germany
2      Italy
0      Spain
0.5    Greece

我希望在满足条件的行的下面一行中添加国家/地区的另一列。如果在excel中假设“ Number”是单元格A1,则公式为: = if(A2> 0,B3,“”)

这样答案将是:

Number Country  New Column
1      Germany  Italy
2      Italy    Spain
0      Spain
0.5    Greece

您如何在熊猫数据框中执行此操作?

我尝试使用:

df["New Column"] = np.where(df["Number"] > 0.5, df["Country"], "")

但这并不表示下面的国家/地区。

3 个答案:

答案 0 :(得分:1)

几乎在<QueryDict: {'name': ['MyProduct'], 'created_on': ['year', 'month', 'day']}>表达式中以{/ {1}}的索引作为必需值/设置为真,而shift表达式中为np.nan或空白" "为false

np.where

答案 1 :(得分:0)

void Boba::printDrink()
{
    cout << "Boba with flavor " << flavor << ", " << toppings << " toppings";
    if (isLarge)
        cout << ", size large";
    cout << ". $" << getPrice() << endl;
}

double Boba::getPrice()
{
    updatePrice();
    return price;
}

答案 2 :(得分:0)

您可以使用shift(-1)用上一行的值创建列

df['New Column'] = df['Country'].shift(-1)

以及以后使用删除某些值的规则

df.loc[ df["Number"] <= 0.5, "New Column"] = ""

最小工作代码

text = '''Number Country
1      Germany
2      Italy
0      Spain
0.5    Greece'''

import pandas as pd
import io

df = pd.read_csv(io.StringIO(text), sep='\s+')

df['New Column'] = df['Country'].shift(-1)
print(df)

df.loc[ df["Number"] <= 0.5, "New Column"] = ""
print(df)

结果

   Number  Country New Column
0     1.0  Germany      Italy
1     2.0    Italy      Spain
2     0.0    Spain     Greece
3     0.5   Greece        NaN

   Number  Country New Column
0     1.0  Germany      Italy
1     2.0    Italy      Spain
2     0.0    Spain           
3     0.5   Greece