赞
踩
目录
5.1 DataFrame.at[行索引,列索引]获取某一个值
5.2 DataFrame.iat[默认行索引,默认列索引]获取某一个值
5.3 DataFrame.loc[行索引,列索引]获取某个值,与at不同的是,只输入某一参数,获得某一行或某一列
5.4 DataFrame.iloc[默认行索引,默认列索引]获取某个值,与iat不同的是,只输入某一参数,获得某一行或某一列:
- data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada','Nevada'],
- 'year':[2000,2001,2002,2003,2004,2005],
- 'pop':[1.5,1.7,3.6,2.4,2.9,3.2]}
- frame = pd.DataFrame(data)
2 按列遍历
- column_indexs = []
- for column_index, row_data in frame.iteritems():
- column_indexs.append(column_index)
- print(row_data)
- print(column_indexs)
运行结果:
- row_indexs = []
- for index, row in frame.iterrows():
- row_indexs.append(index)
- print(row)
- print(row_indexs)
运行结果:
还可以通过列名获取对应数据
- for index, row in frame.iterrows():
- print(row['pop'])
运行结果:
- for row in frame.itertuples():
- print(getattr(row, 'state'), getattr(row, 'year'), getattr(row, 'pop'))
- print(type(row))
运行结果:
演示数据准备
- data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada','Nevada'],
- 'year':[2000,2001,2002,2003,2004,2005],
- 'pop':[1.5,1.7,3.6,2.4,2.9,3.2]}
- frame = pd.DataFrame(data)
4.1 获取frame的index属性,然后使用frame[列索引].get(行索引)获得对应的值
- print(frame.columns)
- for index in frame.index:
- print(frame['state'].get(index))
运行结果:
与上面等价的两种写法
- # 第一种
- for index in frame.index:
- print(frame['state'][index])
- # 第二种
- for index in frame.index:
- print(frame.get('state').get(index))
-
4.2 获取frame的column属性,然后使用frame[列索引].get(行索引)获得对应的值
- print(frame.index)
- for column in frame.columns:
- print(frame[column].get(0))
运行结果:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。