1、从大数据集中获取样本数据
train_data = pd.read_csv('./titanic/train.csv', skiprows= lambda x: x>0 and np.random.rand() > 0.01)
2、dataframe的行列转换
d = {\
"zip_code": [12345, 56789, 101112, 131415],
"factory": [100, 400, 500, 600],
"warehouse": [200, 300, 400, 500],
"retail": [1, 2, 3, 4]
}
df = pd.DataFrame(d)
print(df)
df = df.melt(id_vars='zip_code', value_name= 'name', var_name='value')
print(df)
zip_code factory warehouse retail
0 12345 100 200 1
1 56789 400 300 2
2 101112 500 400 3
3 131415 600 500 4
zip_code value name
0 12345 factory 100
1 56789 factory 400
2 101112 factory 500
3 131415 factory 600
4 12345 warehouse 200
5 56789 warehouse 300
6 101112 warehouse 400
7 131415 warehouse 500
8