熊猫合并数据框创建南列

时间:2019-07-13 08:56:15

标签: python pandas dataframe

我正在尝试将3x2 excel(3x组数据,男性和女性一组)合并到一个大数据框中。每个excel可以包含不同的人。

每个数据框当前存在4列:LidnummerSpelerClubKlassement

每个excel看起来都像下面的

| Lidnummer | Speler | Club | Klassement |
|-----------|--------|------|------------|
| 1         | some1  | meh  | A          |
| 2         | some2  | meh  | D          |
| 3         | some3  | meh  | B2         |

每个性别和每个学科(sxd)都存在

所以我写了以下代码块来读取每个数据集

single_male = pd.read_excel(xlxs, sheet_name=0)[['Lidnummer', 'Speler', 'Club', 'Klassement']].rename(index=str, columns={'Klassement': 's'}).assign(d=np.nan, x=np.nan, gender='M')
single_female = pd.read_excel(xlxs, sheet_name=1)[['Lidnummer','Speler', 'Club', 'Klassement']].rename(index=str, columns={'Klassement': 's'}).assign(d=np.nan, x=np.nan, gender='F')
double_male = pd.read_excel(xlxs, sheet_name=2)[['Lidnummer','Speler', 'Club', 'Klassement']].rename(index=str, columns={'Klassement': 'd'}).assign(s=np.nan, x=np.nan, gender='M')
double_female = pd.read_excel(xlxs, sheet_name=3)[['Lidnummer','Speler', 'Club', 'Klassement']].rename(index=str, columns={'Klassement': 'd'}).assign(s=np.nan, x=np.nan, gender='F')
mix_male = pd.read_excel(xlxs, sheet_name=4)[['Lidnummer','Speler', 'Club', 'Klassement']].rename(index=str, columns={'Klassement': 'x'}).assign(d=np.nan, s=np.nan, gender='M')
mix_female = pd.read_excel(xlxs, sheet_name=5)[['Lidnummer','Speler', 'Club', 'Klassement']].rename(index=str, columns={'Klassement': 'x'}).assign(d=np.nan, s=np.nan, gender='F')

这将合并我认为需要的数据。我将其合并如下

dataFrames = [single_male, single_female, double_male, double_female, mix_male, mix_female]
players = reduce(lambda left, right: pd.merge(left, right, on=['Lidnummer','Speler', 'Club', 'gender'], how='outer'), dataFrames)
players.head()

这似乎可行,除了它创建了列s_xs_yd_xd_yx_xx_y。然后将每个学科(s,d和x)的数据分为两列。

一些谷歌搜索建议包含例如's'在合并的on=部分中,但随后出现错误

You are trying to merge on object and float64 columns. If you wish to proceed you should use pd.concat

我尝试使用concat,但无法正常工作。

那么我如何为sdx制作一列,其中包含每个学科的数据?

因此,结果数据集将如下所示:

| Lidnummer | Speler | Club | gender | s  | d   | x  |
|-----------|--------|------|--------|----|-----|----|
| 1         | some1  | meh  | M      | A  | A   | A  |
| 2         | some2  | meh  | F      | D  | C2  | C1 |
| 3         | some3  | meh  | F      | B2 | B1  | B2 |

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码(此处没有excel)吗?

columns= ['Lidnummer', 'Speler', 'Club', 'Klassement']
single_male = pd.read_excel(xlxs, sheet_name=0)[columns]
single_male['gender']='M'

single_female = pd.read_excel(xlxs, sheet_name=1)[columns]
single_male['gender']='F'

double_male = pd.read_excel(xlxs, sheet_name=2)[columns]
single_male['gender']='M'

double_female = pd.read_excel(xlxs, sheet_name=3)[columns]
single_male['gender']='F'

mix_male = pd.read_excel(xlxs, sheet_name=4)[columns]
single_male['gender']='M'

mix_female = pd.read_excel(xlxs, sheet_name=5)[columns]
single_male['gender']='F'

all= pd.concat([single_male, single_female, double_male. double_female, mix_male, mix_female], axis='index', ignore_index=True)

all.rename({'Klassement': 's'}, axis='columns', inplace=True)
all['d']= all['s']
all['x']= all['s']