Pandas

1、excel 操作

    ID      Name  InStore  Date
0  NaN  Book_001      NaN   NaN
1  NaN  Book_002      NaN   NaN
2  NaN  Book_003      NaN   NaN
3  NaN  Book_004      NaN   NaN
4  NaN  Book_005      NaN   NaN
5  NaN  Book_006      NaN   NaN
6  NaN  Book_007      NaN   NaN
7  NaN  Book_008      NaN   NaN
8  NaN  Book_009      NaN   NaN

import pandas as pd
from datetime import date,timedelta

def add_month(d,md):
    yd = md // 12
    m  = d.month + md % 12
    if m != 12:
        yd += m // 12
        m = m % 12
    return  date(d.year + yd,m,d.day)
books = pd.read_excel(r'C:\Users\Lenovo\Desktop\excel\Books_data.xlsx',skiprows=3,usecols='C:F',index_col=None
                      ,dtype={'ID':str,'InStore':str,'Date':str})
start = date(2019,1,1)

for i in  books.index:
    books['ID'].at[i] = i + 1
    books['InStore'].at[i] = 'Yes'if i % 2 == 0 else 'N0'
    books['Date'].at[i] = start
    # books['Date'].at[i] = start + timedelta(days=i)
    # books['Date'].at[i] = date(start.year + i,start.month,start.day)
    books['Date'].at[i] = add_month(start,i)
print(books)


    ID      Name InStore        Date
0    1  Book_001     Yes  2019-01-01
1    2  Book_002      N0  2019-02-01
2    3  Book_003     Yes  2019-03-01
3    4  Book_004      N0  2019-04-01
4    5  Book_005     Yes  2019-05-01
5    6  Book_006      N0  2019-06-01
6    7  Book_007     Yes  2019-07-01
7    8  Book_008      N0  2019-08-01
8    9  Book_009     Yes  2019-09-01
9   10  Book_010      N0  2019-10-01
10  11  Book_011     Yes  2019-11-01
11  12  Book_012      N0  2019-12-01
12  13  Book_013     Yes  2020-01-01
13  14  Book_014      N0  2020-02-01
14  15  Book_015     Yes  2020-03-01
# 列运算
# 具体某列值运算
for i in range(2,6):
    books['Price'].at[i] = books['Price'].at[i] + 2
# # books['Price']= books['Price'] .apply(函数名)  # 自定义一个函数方式
# books['Price']= books['Price'] .apply(lambda x:x+2)

 

2、时间日期处理小结(datetime模块)

https://www.cnblogs.com/tianyiliang/p/8270509.html

3、排序

import pandas as pd

products = pd.read_excel(r'C:\Users\Lenovo\Desktop\excel\temp\List.xlsx', index_col='ID')
products.sort_values(by=['Worthy', 'Price'], ascending=[True, False], inplace=True)
print(products)

 


posted @ 2019-10-13 10:10  蜡哲图  阅读(154)  评论(0编辑  收藏  举报
目录内容