如下所示:
# create a dataframe with an integer feature and a categorical string feature
demo_df = pd.DataFrame({'Integer Feature': [0, 1, 2, 1], 'Categorical Feature': ['socks', 'fox', 'socks', 'box']})
demo_df
接下来用for遍历:
for indexs in demo_df.index:
假设我们有一个很简单的OTU表:
现在对这个表格进行遍历,一般写法为:
import pandas as pd
otu = pd.read_csv("otu.txt",sep="\t")
for index,row in otu.iterrows():
print index
print row
这里的iterrows()返回值为元组,(index,row)
上面的代码里,for循环定义了两个变量,index,row,那么返回的元组,index=index,row=row.
如果fo
在做分类模型时候,需要在DataFrame中按照行获取数据以便于进行训练和测试。
import pandas as pd
dict=[[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8],[4,5,6,7,8,9],[5,6,7,8,9,10]]
data=pd.DataFrame(dict)
print(data)
for indexs in data.index:
print(data.loc[indexs].values[0:-1])
实验结果:
/u