2016-09-04 47 views
0

我使用Python 3.4.4和Pandas 0.18.1來確定實驗數據的置信區間。這導致了對數據幀列的許多計算。 從Pandas doc推薦使用鏈接[]方法的.loc []方法,但似乎無法應用。這裏用一個簡化的數據幀的一例爲了使用Python中的列來計算索引以及熊貓數據框

df1 = pd.DataFrame(np.array([[1,2,3],[4,5,6],[7,8,9]]), index=['a','b','c'], columns=['A','B','C']) 
print(df1) 
    A B C 
a 1 2 3 
b 4 5 6 
c 7 8 9 

要在新列「E」我嘗試

df1.loc[:,'E'] = df1.loc[:,['A']]*3 
print(df1) 
    A B C E 
a 1 2 3 NaN 
b 4 5 6 NaN 
c 7 8 9 NaN 

計算列「A」次3如果使用未推薦的方法我獲得

df1.loc[:,'E'] = df1['A']*3 
print(df1) 
    A B C E 
a 1 2 3 3 
b 4 5 6 12 
c 7 8 9 21 

因此它看起來像第二種方法是很好的,但我的大數據幀我得到

我花了很多時間去尋找一個解決方案satifying「SettingWithCopyWarning值要試圖從一個數據幀切片的副本設置」,無果而終。
非常感謝您的幫助。

+2

'df1 ['E'] = df1 ['A'] * 3'是創建新列的更爲標準的方法。 – Psidom

回答

0
import pandas as pd 
import numpy as np 
df1 = pd.DataFrame(np.array([[1,2,3],[4,5,6],[7,8,9]]), index=['a','b','c'], columns=['A','B','C']) 
print(df1) 
df1['E']= df1['A']*3 
print(df1) 

output: 
    A B C 
a 1 2 3 
b 4 5 6 
c 7 8 9 
    A B C E 
a 1 2 3 3 
b 4 5 6 12 
c 7 8 9 21