data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada','Nevada'], 'year': [2000,2001,2002,2001,2002,2003], 'pop':[1.5,1.7,3.6,2.4,2.9,3.2]} frame = pd.DataFrame(data, columns=['year','state','pop','debt'], index=['one','two','three','four','five','six']) print(frame) ==> year state pop debt one 2000 Ohio 1.5 NaN two 2001 Ohio 1.7 NaN three 2002 Ohio 3.6 NaN four 2001 Nevada 2.4 NaN five 2002 Nevada 2.9 NaN six 2003 Nevada 3.2 NaN
对于DataFrame中的一列,可以按字典型标记或属性检索为Series
1 2 3 4 5 6 7 8 9 10 11 12
data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada','Nevada'], 'year': [2000,2001,2002,2001,2002,2003], 'pop':[1.5,1.7,3.6,2.4,2.9,3.2]} frame = pd.DataFrame(data, columns=['year','state','pop','debt'], index=['one','two','three','four','five','six']) print(frame[state]) ==> one Ohio two Ohio three Ohio four Nevada five Nevada six Nevada Name: state, dtype: object
data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada','Nevada'], 'year': [2000,2001,2002,2001,2002,2003], 'pop':[1.5,1.7,3.6,2.4,2.9,3.2]} frame = pd.DataFrame(data, columns=['year','state','pop','debt'], index=['one','two','three','four','five','six']) frame['debt']=16.5 print(frame) ==> year state pop debt one 2000 Ohio 1.516.5 two 2001 Ohio 1.716.5 three 2002 Ohio 3.616.5 four 2001 Nevada 2.416.5 five 2002 Nevada 2.916.5 six 2003 Nevada 3.216.5 frame['debt']= np.arange(6.) print(frame) ==> year state pop debt one 2000 Ohio 1.50.0 two 2001 Ohio 1.71.0 three 2002 Ohio 3.62.0 four 2001 Nevada 2.43.0 five 2002 Nevada 2.94.0 six 2003 Nevada 3.25.0
data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada','Nevada'], 'year': [2000,2001,2002,2001,2002,2003], 'pop':[1.5,1.7,3.6,2.4,2.9,3.2]} frame = pd.DataFrame(data, columns=['year','state','pop','debt'], index=['one','two','three','four','five','six']) val = pd.Series([-1.2,-1.5,-1.7],index = ['two','four','five']) frame['debt'] = val print(frame) ==> year state pop debt one 2000 Ohio 1.5 NaN two 2001 Ohio 1.7-1.2 three 2002 Ohio 3.6 NaN four 2001 Nevada 2.4-1.5 five 2002 Nevada 2.9-1.7 six 2003 Nevada 3.2 NaN