码迷,mamicode.com
首页 > 其他好文 > 详细

pandas入门

时间:2019-03-30 12:07:53      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:col   索引   tail   eric   japan   多列   int   series   大写   

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

pandas入门

标签:col   索引   tail   eric   japan   多列   int   series   大写   

原文地址:https://blog.51cto.com/14156081/2371569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!