微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pandas入门

建立pandas

import 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"])#混合索引

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐