重学pyhtonpandas

Posted 信小呆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重学pyhtonpandas相关的知识,希望对你有一定的参考价值。

#-*- encoding:utf-8 -*-
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
#认识Series
x1 = Series([1, 2, 3])
print(x1)
x2 = Series([1, 2, 3],index=["a", "b", "c"])
print(x2)
d = 'a': 3, 'b': 2, 'c': 1
x3 = Series(d)
print(x3)

#认识DataFrame
data = 'chinese': [90, 98, 91], 'math': [93, 91, 83], 'english': [None, 82, 72]
df1 = DataFrame(data)
print(df1)
df2 = DataFrame(data, index=['小瑞', '小田', '小李'])
print(df2)
print(df2['chinese']['小李'])

#读取excel的数据
score = DataFrame(pd.read_excel('/Users/liyong/PycharmProjects/pythonProject/data1.xlsx'))
#####df2.to_excel('/Users/liyong/PycharmProjects/pythonProject/data1.xlsx')
print(score)

#数据清理
#操作df2
#删除english
df3 = df2.drop(columns=["english"])
print(df3)
#删除小李
df4 = df2.drop(index=["小李"])
print(df4)
#修改列名
df5 = df2.rename(columns='chinese': 'Yu Wen')
print(df5)
#删除重复行
df2.drop_duplicates()
#修改数据格式
df2['chinese'] = df2['chinese'].astype(np.str)
print(df2)

#删除左右两边空格
df2['chinese'] = df2['chinese'].map(str.strip)
#删除左边空格
df2['chinese'] = df2['chinese'].map(str.lstrip)
#删除右边空格
df2['chinese'] = df2['chinese'].map(str.rstrip)


#全部大写
df2.columns = df2.columns.str.upper()
#全部小写
df2.columns = df2.columns.str.lower()
#首字母大写
df2.columns = df2.columns.str.title()

print(df2)

#查找空
print(df2.isnull())

#apply 函数对数据进行清洗
def double_df2(x):
    return 2*x
df2['Math'] = df2['Math'].apply(double_df2)
print(df2)

#清理None
def clean_df2(x):
    if x != x:
        return 0
    else:
        return x
df2['English'] = df2['English'].apply(clean_df2)
#计算总成绩
df2['Chinese'] = df2['Chinese'].astype(np.int64)
def count(df):
    df['Count'] = df['Chinese']+ df['Math']+ df['English']
    return df
df2 = df2.apply(count,axis=1)
print(df2)

##数据统计
#Pandas和NumPy用法差不多

df7 = DataFrame('name':['ly', 'ty', 'lr', 'hd', 'cp'], 'data1':range(5))
print(df7)
print(df7.describe())


#数据表合并

df1 = DataFrame('name':['liyon', 'lvru', 'a', 'b', 'c'], 'data1':range(5))
df2 = DataFrame('name':['liyon', 'lvru', 'A', 'B', 'C'], 'data2':range(5))
#基于某列进行链接
df3 = pd.merge(df1, df2, on='name',how='left')
print(df3)

df3 = pd.merge(df1, df2, on='name',how='outer')
print(df3)

#使用sql打开pandas
from pandasql import sqldf, load_meat, load_births

pysqldf = lambda sql: sqldf(sql,globals())
sql = "select * from df1 where name='liyon'"
print(pysqldf(sql))


#练习并对数据进行清洗。同时新增一列“总和”计算每个人的三科成绩之和。
data = '语文': [66, 95, 95, 90, 80, 80], '英语': [65, 85, 92, 88, 90, 90], '数学': [None, 98, 96, 77, 90, 90]
df6 = DataFrame(data,index=["张飞", "关羽", "赵云", "黄忠", "典韦", "典韦"])
print(df6)
#删除重复行
df6 = df6.drop_duplicates()
print(df6)
for i in df6.columns:
    df6[i] = df6[i].apply(clean_df2)
print(df6)
def count(df):
    df['总成绩'] = df['语文']+ df['英语']+ df['数学']
    return df
df6 = df6.apply(count,axis=1)
print(df6)

不要太在意别人的评价,做自己喜欢的事情就好~温故而知新嘛

以上是关于重学pyhtonpandas的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 371. 两整数之和(异或操作) / 639. 解码方法 II(动态规划)/ 437. 路径总和 III

[LC] 112题 路径总和(在二叉树里判断是否有哪条路径之和等于某个值)

学生各科目总分之和

拼字游戏

向显示幅度的数据帧添加一行(正值或负值之和的最大值)

近似 R 中二项式随机变量之和的分布