首页 > 解决方案 > 如何在“团队”列中洗牌“爱国者”和“小马”标签?(PYTHON)

问题描述

假设这是数据,我如何洗牌“团队”列中的标签“爱国者”和“小马队”并将其附加到新列?

Team  Blakeman  Prioleau    Combined    Pressure Drop
 0  Patriots    11.50   11.80   11.650  0.850
 1  Patriots    10.85   11.20   11.025  1.475
 2  Patriots    11.15   11.50   11.325  1.175
 3  Patriots    10.70   11.00   10.850  1.650
 4  Patriots    11.10   11.45   11.275  1.225
 5  Patriots    11.60   11.95   11.775  0.725
 6  Patriots    11.85   12.30   12.075  0.425
 7  Patriots    11.10   11.55   11.325  1.175
 8  Patriots    10.95   11.35   11.150  1.350
 9  Patriots    10.50   10.90   10.700  1.800
10  Patriots    10.90   11.35   11.125  1.375
11   Colts      12.70   12.35   12.525  0.475
12   Colts      12.75   12.30   12.525  0.475
13   Colts      12.50   12.95   12.725  0.275
14   Colts      12.55   12.15   12.350  0.650

标签: python

解决方案


你在寻找这样的东西吗?

import pandas as pd
import numpy as np

df = pd.DataFrame({'Team': ['Patriots','Patriots','Colts'], 'Blakeman':[11.50,10.85,12.70]})
df['new_col'] = np.random.permutation(df['Team'].values)
df
    Team      Blakeman  new_col
0   Patriots  11.50     Colts
1   Patriots  10.85     Patriots
2   Colts     12.70     Patriots

推荐阅读