Python 使用pandas读取文件以及基本处理

1. 打印前几条数据.

import pandas 
data = pandas.read_csv('user.csv')
data.head(5)

2. 打印数据详细信息.

import pandas 
data = pandas.read_csv('user.csv')
print (data.describe())


3.获取一个中间值(平均值)

import pandas 
data = pandas.read_csv('user.csv')
print (data['userAge'].median())

4.使用中间值填充csv文件中的缺失值

import pandas 
data = pandas.read_csv('user.csv')
data['userAge'] = data['userAge'].fillna(data['userAge'].median())
data.head(5)

5.找出数据列当中的包含的值

import pandas 
data = pandas.read_csv('user.csv')
print (data['userName'].unique())


6.读取操作Excel

import pandas as pd

def tax(s):
    ss = s - 3500
    if ss <= 0:
        return 0
    elif ss < 1500:
        return ss * 0.03
    elif ss < 4500:
        return ss * 0.1 - 105
    elif ss < 9000:
        return ss * 0.2 - 555
    elif ss < 35000:
        return ss * 0.25 - 1005
    elif ss < 55000:
        return ss * 0.3 - 2755
    elif ss < 80000:
        return ss * 0.35 - 5505
    else:
        return ss * 0.45 - 13505
            
df = pd.read_excel('salary.xlsx', sheetname = 0)
ts = []
for s in df['工资']:
    ts.append(tax(s))
print(ts)
df['税'] = ts

out = pd.ExcelWriter('salary_and_tax.xls')
df.to_excel(out)
out.save()






你可能感兴趣的:(Python)