读取文档

  1. import pandas as pd
  2. csv= pd.read_csv(r'D:\python code\data_202109.tsv',sep='\t',nrows=200000,encoding='gb18030')
  3. csv.head(10)
  1. import pandas as pd
  2. hxx="D:/python code/6_0csv.csv"
  3. csv=pd.read_csv(hxx)
  4. csv.head()
  1. import pandas as pd
  2. hxx="D:/python code/6_0.xlsx"
  3. xlsx=pd.read_excel(hxx)
  4. xlsx

给文件表头

  1. hxx="D:/python code/hwt_5.txt"
  2. txt=pd.read_csv(
  3. hxx,
  4. sep="\t",
  5. header=None,
  6. names=['ggg','2','3','4','5','6','7','8','9','10','11']
  7. )
  8. txt

判断空值

  1. csv.isnull()
  2. csv["品牌_NAME()表头"].isnull()
  3. csv["品牌_NAME"].notnull()
  4. csv.loc[:,'品牌_NAME']=csv['品牌_NAME'].fillna(0)
  5. csv.dropna(axis="index",how="any",inplace=True)
  6. csv["品牌_NAME"].str.replace("other/其他","0")

保存文档

  1. csv.to_csv("D:/python code/6_00csv.csv",index=False)
  2. csv3.to_excel("D:/python code/fenlei.xlsx",index=False)

待研究

  1. csv.shape
  2. csv.columns
  3. csv.index
  4. csv["品牌_NAME"].dtypes
  5. csv.plot()

特定输出

  1. csv.loc[0:10,'商品ID']
  2. csv["商品月销量"]>1000
  3. csv.loc[(csv["商品月销量"]>500)&(csv["商品价格"]>50), :]
  4. csv.loc[(csv["商品月销量"]>500)&(csv["商品价格"]<11), :]
  5. csv1=csv.loc[(csv["一级类目"]=='服装鞋包')]

重新打标签

  1. def get_yichang1(x):
  2. if x["商品月销量"]<500:
  3. return '可能正常'
  4. if x["商品月销量"]>10000:
  5. return '一定异常'
  6. return '可能异常'
  7. csv.loc[:, "yichang1"]=csv.apply(get_yichang1,axis=1)
  8. csv["yichang1"].value_counts()
  1. def get_Pinpai(x):
  2. if x["品牌_NAME"]=='1':
  3. return '1'
  4. if x["品牌_NAME"]=='0':
  5. return '0'
  6. return '2'
  7. csv.loc[:, "Pinpai"]=csv.apply(get_Pinpai,axis=1)
  1. def get_yichang2(x):
  2. if x["yichang1"]=='1' and x["Pinpai"]=='1':
  3. return '0'
  4. if x["yichang1"]=='1' and x["Pinpai"]=='0':
  5. return '0'
  6. if x["yichang1"]=='0':
  7. return '0'
  8. return '1'
  9. csv.loc[:, "yichang2"]=csv.apply(get_yichang2,axis=1)
  1. def get_yichang3(x):
  2. if x["店铺月销量"]/x["商品月销量"]<10:
  3. return '0'
  4. return '1'
  5. csv.loc[:, "yichang3"]=csv.apply(get_yichang3,axis=1)

查看元素并计数

  1. csv["一级类目"].unique()
  2. csv["一级类目"].value_counts()
  3. csv.plot()

作图

  1. csv_xy=csv.describe()
  2. csv.plot()
  3. import pandas as pd
  4. import numpy as np
  5. %matplotlib inline
  6. csv_xy2=csv.groupby('一级类目').sum()
  7. csv_xy2
  8. csv_xy2.plot()

读列读行

  1. xlsx['月份']//列
  2. xlsx.loc[1]//行

更改数据

  1. import pandas as pd
  2. data = pd.read_csv(r'D:\python code\shuju.csv')
  3. # print(data)
  4. # 方法一:replace
  5. a=data.replace("other/其他",1)//其他 改为 1
  6. print(a)
  7. a.to_excel("D:/python code/shuju_0.xlsx",index=False)

模型代码

  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn.ensemble import IsolationForest
  5. from scipy import stats
  6. import itertools
  7. import matplotlib
  8. from sklearn.model_selection import train_test_split
  9. from sklearn.preprocessing import MinMaxScaler
  10. import warnings
  11. hwt="D:/python code/shuju_03.csv"
  12. csv=pd.read_csv(hwt,index_col="商品ID")
  13. csv
  14. csv.index
  15. csv.sort_index(inplace=True)//排序
  16. csv.index//查看
  17. print("特征列表:",list(csv.columns))//查看特征列表
  18. Ilf = IsolationForest(max_samples=500,contamination=0.15,max_features=1.0,random_state=200, verbose=0)//重要参数
  19. Ilf.fit(csv)
  20. y_pred = Ilf.predict(csv)
  21. y_pred = pd.DataFrame(y_pred, columns=['result'])
  22. y_pred[y_pred['result'] == -1].index.values
  23. csv = csv.iloc[y_pred[y_pred['result'] == -1].index.values]
  24. csv.reset_index(drop=False, inplace=True)
  25. print("正常数据的数量 :", y_pred[y_pred['result'] == 1].shape[0])
  26. print("异常数据的数量 :", csv.shape[0])//分组
  27. train_col = list(csv.columns)
  28. mat_train = np.matrix(csv)
  29. mat_new = np.matrix(csv.drop('商品ID', axis=1))
  30. mat_y = np.array(csv.商品ID).reshape((150, 1))
  31. prepro_y = MinMaxScaler()
  32. prepro_y.fit(mat_y)
  33. prepro = MinMaxScaler()
  34. prepro.fit(mat_train)
  35. csv1 = pd.DataFrame(prepro.transform(mat_train), columns=train_col)
  36. print(csv1.head())
  37. COLUMNS = train_col
  38. LABEL = "商品ID"
  39. training_set = train[COLUMNS]
  40. prediction_set = train.商品ID
  41. X=csv1[['月份', '商品价格', '商品月销量', '商品月销售额', '店铺ID', '店铺月销量', '店铺月销售额', '商品描述得分', '服务得分', '物流得分']]
  42. y=csv1['商品ID']
  43. from sklearn import model_selection
  44. # 将数据集拆分为训练集和测试集
  45. X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size = 0.2, random_state = 1234)
  46. X_train
  47. X_test
  48. y_test