前段时间学习了pandas,也做了一些练习,今天做一下梳理。
pandas 中对于axis的理解
在对dataframe操作的时候,很多时候涉及到了axis的设置,我找到一幅图很好的能理解axis:
数据准备
读取otu.txt表并且选取5行5列
import pandas as pd
df = pd.read_csv("otu_taxon.txt",header=0,index_col=0,sep="\t")
df = df.iloc[0:5,0:5]
df.to_csv("otu.txt",sep="\t")
现在的otu.txt:
$ cat otu.txt
OTU A1 A2 A3 A4 A5
OTU_1 102 111 221 98 70
OTU_2 13 1 39 22 1
OTU_3 8508 8208 8165 8882 7499
OTU_4 2122 1881 2414 2520 1923
OTU_5 7700 7442 11718 6392 7546
单独选取其中一列
df[“colname”] or df.colname
import pandas as pd
df = pd.read_csv("otu.txt",header=0,index_col=0,sep="\t")
A1 = df["A1"]
print "A1 = df['A1']:",A1
A1 = df.A1
print "A1 = df.A1",A1
两种方法的输出结果:
$ python pand.py
A1 = df['A1']: OTU
OTU_1 102
OTU_2 13
OTU_3 8508
OTU_4 2122
OTU_5 7700
Name: A1, dtype: int64
A1 = df.A1 OTU
OTU_1 102
OTU_2