小编djj*_*djj的帖子

在pandas DataFrame中设置新列的正确方法是为了避免SettingWithCopyWarning

试图在netc df中创建一个新列,但我收到了警告

netc["DeltaAMPP"] = netc.LOAD_AM - netc.VPP12_AM

C:\Anaconda\lib\site-packages\ipykernel\__main__.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Run Code Online (Sandbox Code Playgroud)

在新版本的Pandas中创建一个字段的正确方法是什么,以避免收到警告?

pd.__version__
Out[45]:
u'0.19.2+0.g825876c.dirty'
Run Code Online (Sandbox Code Playgroud)

python pandas

18
推荐指数
4
解决办法
7383
查看次数

从Pandas中获取两个返回值

我试图从apply方法中返回两个不同的值,但我无法弄清楚如何获得我需要的结果.

功能如下:

def fun(row):
    s = [sum(row[i:i+2]) for i in range (len(row) -1)]
    ps = s.index(max(s))
    return max(s),ps
Run Code Online (Sandbox Code Playgroud)

df作为:

    6:00    6:15    6:30    
0   3       8       9       
1   60      62      116     
Run Code Online (Sandbox Code Playgroud)

我试图返回行的最大值,但我还需要获得产生最大组合的第一个值的索引.

df["phour"] = t.apply(fun, axis=1)
Run Code Online (Sandbox Code Playgroud)

我可以得到我需要的输出,但我不知道如何在新列中获取索引.到目前为止,我得到的答案都是 tuple

    6:00    6:15    6:30    phour
0   3       8       9       (17, 1)
1   60      62      116     (178, 1)
Run Code Online (Sandbox Code Playgroud)

如何在自己的列中获取索引值?

python pandas

4
推荐指数
2
解决办法
2299
查看次数

标签 统计

pandas ×2

python ×2