pandas入门
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas入门相关的知识,希望对你有一定的参考价值。
建立pandasimport pandas as pd
import numpy as np
p=pd.Series(range(1,12))#Series大写
print(p)
p=pd.Series(np.random.rand(4))
print(p)
p=pd.Series({"A":1,"B":32})
print(p)
p=pd.Series(np.random.rand(4),index=["a","b","c","d"])
print(p)
数据预览
p=pd.Series(np.random.rand(4),index=["a","b","c","d"])
print(p.head(2))
print(p.tail(2))
数据索引
import pandas as pd
import numpy as np
p=pd.Series(np.random.rand(400))
print(p.head(2))
print(p.tail(2))
print(p.index)
print(p.name)
print(p.index.name)
p.index.name="index"
p.name="p_num"
print(p)
位置
import pandas as pd
import numpy as np
p=pd.Series(np.random.rand(5))
print(p)
print(p[2])
print(p.loc[2])
print(p.iloc[2])#当索引为数字的时候,三者一样
构建dataframe
import pandas as pd
import numpy as np
a=np.ceil(np.random.rand(71,4)*149)
df=pd.DataFrame(a)
print(df.head(4))
df.index#行名
df.columns#列名
改行名字和索引
import pandas as pd
import numpy as np
a=np.ceil(np.random.rand(71,4)*149)
df=pd.DataFrame(a)
df.columns=["a","b","c","d"]#改行名字
a=df.columns
df=df.drop(columns=["c","d"])#丢弃cd列
df["E"]=np.ceil(np.random.rand(71,1)*149)#增加一列
print(df["a"])#按名字取行
print(df.head(4))
重置index
import pandas as pd
import numpy as np
a=np.ceil(np.random.rand(71,4)*149)
df=pd.DataFrame(a)
df.columns=["a","b","c","d"]#改行名字
a=df.columns
df=df.drop(columns=["c","d"])#丢弃cd列
df["E"]=np.ceil(np.random.rand(71,1)*149)#增加一列
print(df["a"])#按名字取行
print(df.head(4))
print(df[32:52])#取行,用逗号不行
d=df[32:45]
print(d)
d=d.reset_index()#重置index
print(d)
dataframe索引
import pandas as pd
import numpy as np
c1=pd.Series({"name":"china","language":"c","AREA":12321})
c3=pd.Series({"name":"america","AREA":18321,"language":"e"})
c2=pd.Series({"name":"japan","AREA":191,"language":"j"})
df=pd.DataFrame([c1,c2,c3],index=["c","a","j"])
print(df)
print(df["AREA"])#取列
print(df[["AREA","language"]])#取多列
print(df.loc["c"])#行名字取行
print(df.iloc[0])#行索引取行
print(df.iloc[0]["language"])#混合索引
print(df.loc["c"]["language"])#混合索引
以上是关于pandas入门的主要内容,如果未能解决你的问题,请参考以下文章
pandas GroupBy上的方法apply:一般性的“拆分-应用-合并”