0

我有像下面这样的数据框,并希望在 pandas 中通过“应​​用方法”使用下面的 def 更改为下面的结果 df。
据我所知,“应用”方法使一系列不取代原始 df。

id a b
-------
a  1 4
b  2 5
c  6 2

if df['a'] > df['b'] :
    df['a'] = df['b']
else :
    df['b'] = df['a']

result df :

id a b
-------
a  4 4
b  5 5
c  6 6
4

3 回答 3

2

我不确定你需要什么,因为预期的输出与你的情况不同,这里我只能修复你的代码

for x,y in df.iterrows():
    if y['a'] > y['b']:
        df.loc[x,'a'] = df.loc[x,'b']
    else:
        df.loc[x,'b'] = df.loc[x,'a']

df
Out[40]: 
  id  a  b
0  a  1  1
1  b  2  2
2  c  2  2

如果我正确理解您的问题

df.assign(**dict.fromkeys(['a','b'],np.where(df.a>df.b,df.a,df.b)))
Out[43]: 
  id  a  b
0  a  1  1
1  b  2  2
2  c  2  2
于 2019-02-21T04:26:21.170 回答
1

像其他人一样,不完全确定您要做什么,我将假设您的意思是将当前“A”或“B”值的值设置为等于任一列的最高值该行中的值....如果该假设是正确的,那么使用“.apply()”可以做到这一点。

首先,“.apply()”的大多数“干净”应用程序(记住“.apply()”的应用程序通常不推荐使用)使用一个函数,该函数接受由“.apply”提供给它的行的输入。 apply()” 函数,通常返回相同的对象,但根据需要修改/更改/等。考虑到您的数据框,这是一个实现所需输出的函数,然后使用“.apply()”对该数据框应用该函数。

# Create the function to be used within .apply()
def comparer(row):
    if row["a"] > row["b"]:
        row["b"] = row["a"]
    elif row["b"] > row["a"]:
        row["a"] = row["b"]
    return(row)

# Use .apply() to execute our function against our column values. Returning the result of .apply(), re-creating the "df" object as our new modified dataframe.
df = df.apply(comparer, axis=1)

然而,大多数人(如果不是每个人)似乎都反对“.apply()”的使用。我可能会听从他们的智慧:)

于 2019-02-21T05:14:11.397 回答
0

尝试 :

df = pd.DataFrame({'a': [1, 2, 6], 'b': [4,5,2]})

df['a'] = df.max(axis=1)
df['b'] = df['a']
于 2019-02-21T04:51:40.773 回答