Numpy增加一列,指定概率指定參數
阿新 • • 發佈:2019-04-17
nbsp clas value 概率 span col port bsp num
這裏主要應用到numpy.random.choice
可以根據需求,比如增加一列,A B C 為該列數據,隨機概率生成
詳見代碼
import numpy as np import pandas as pd columns_user = [‘number‘,‘gender‘,‘date‘] columns_order = [‘order_id‘,‘user_id‘,‘ispaid‘,‘price‘,‘paidtime‘] user_info = pd.read_csv(‘user_info_utf.csv‘,names=columns_user) order_info= pd.read_csv(‘order_info_utf.csv‘,names = columns_order) # 增加一列gender a = np.random.choice([‘Male‘,‘Female‘],size=len(order_info[‘user_id‘]),p =[0.3,0.7]) order_info[‘gender‘] = pd.Series(a) # 增加一列 商鋪 print(pd.value_counts(order_info[‘gender‘])) b = np.random.choice([‘A‘,‘B‘,‘C‘],size=len(order_info[‘user_id‘]),p=[0.5,0.3,0.2]) order_info[‘shop‘] = pd.Series(b) print(pd.value_counts(order_info[‘shop‘])) print(order_info.head())
Numpy增加一列,指定概率指定參數