pandas数据分析——编程练习100题 我是在B站跟着蚂蚁学python学的课程。
#1、list转series
import pandas as pd
import numpy as np
course = ["Chinese","Math","English","Computer"]
data1 = pd.Series(data=course)
#2、dict转series
grades = {"Chinese":80,"Math":90,"English":85,"Computer":100} data2 = pd.Series(data=grades)
#3、Series转换为list
Numbers = list()
#4、将Series转为DataFrame
df = pd.DataFrame(data2,columns=['grade'])
print(df)
#5、借助numpy创建Series
s = pd.Series(
np.arange(10,100,10),#数值: 10~90,间隔10
index=np.arange(101,110),#索引: 101~109,间隔1lambda编程
dtype='float64'#类型: float64
)
print(s)
#6、转换Series的数据类型,输⼊为字符串类型,要求输出为数值类型s5 = pd.Series(
data=["001","002","003","004"],
index=list("abcd")
)
#s5 = s5.astype(int)
s5 = s5.map(int)
print(s5)
#7、给Series添加元素
data7 = data2.append(pd.Series({"physics":88,"chemic":95})) print(data7)
#8、⽤reset index将Series转换成df
df = set_index()
print(df)
#9、使⽤字典创建DataFrame
df9 = pd.DataFrame(
{
"name":["xiaozhang","xiaowang","xiaoli","xiaozhao"], "gender":["male","female","male","female"],
"age":["18","19","20","18"]
}
)
print(df9)
#10、给DataFrame设置索引列
df9.set_index("name",inplace=True)
print(df9)
#11、⽣成⼀个⽉份所有⽇期
data_range = pd.data_range(start='2021-10-01',end='2021-10-31')
#字符串中提取数值特征
df['mileage'].map(lambda x : float(x.split(" ")[0]))
import re
test = "12.7 @ 2,700(kgm@ rpm)"
def parse_rpm(torque):
torque = place(",","")
return max([float(x) for x in re.findall("\d+",torque)])
df["torque"].map(parse_rpm)
下次更新时间:2022/2/12
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论