拆分系列中的项目

时间:2014-11-17 21:39:45

标签: python pandas

我有一个看起来像这样的系列;

Name: TOR, Length: 162, dtype: object, 
['TOR'], 
0      [W, 9-7]
1      [W, 5-1]
2      [W, 8-2]
3      [L, 1-2]
4      [L, 2-6]
5      [W, 2-1]

等,

数据来自一个pandas数据框,其中每个团队都有一个包含上述数据的列。如何将此系列转换为数据框,其中一列为W或L,另一列为得分,9-7?我甚至可以放弃包括“,”在内的所有内容。

编辑,

来自字典的样本

WinLoss
{'NYY': [[u'L', u'2-6'], [u'L', u'1-3'],....
'MIN': [[u'L', u'3-5'], [u'L', u'6-7'], [u'W', u'10-9']....
'CIN': [[u'L', u'0-1'], [u'W', u'1-0'], [u'L', u'6-7'],

如果我只是

Wintable = pd.DataFrame(WinLoss)

我最终遇到了原始问题

数据框摘录;

>>> Wintable
           ARI        ATL        BAL        BOS        CHC        CHW  \
0     [L, 1-3]   [L, 0-2]   [W, 2-1]   [L, 1-2]   [L, 0-1]   [W, 5-3]   
1     [L, 5-7]   [W, 5-2]   [L, 2-6]   [W, 6-2]   [L, 3-4]   [W, 7-6]   
2     [L, 8-9]   [W, 1-0]   [L, 3-4]   [W, 4-3]   [W, 3-2]  [L, 9-10]   
3     [W, 5-4]   [W, 2-1]  [L, 4-10]   [L, 2-6]   [L, 2-7]   [L, 5-7]   
4     [L, 0-2]   [W, 6-2]   [L, 6-7]   [L, 6-7]   [L, 0-2]   [L, 3-4]   
5     [L, 5-8]   [L, 1-2]   [W, 3-1]   [L, 0-4]   [W, 8-3]   [W, 5-1]   
6    [L, 2-12]   [L, 0-4]   [L, 2-4]   [W, 5-1]   [L, 6-7]   [L, 1-8]   
7     [L, 4-9]   [W, 4-3]  [W, 14-5]  [L, 7-10]   [W, 7-5]  [W, 15-3]   

1 个答案:

答案 0 :(得分:1)

更新

由于OP已经改变了要求,我已经更新了这个答案,以便采取更直接的方法。

这是一个可行的解决方案,可能不是最好的,但是对于一次性它可以完成工作。 不是将系列转换为可用的DataFrame,而是应该处理源代码并读入DataFrame,如下所示:

import pandas as pd

# remember the length of each 'NYC' ... 'CIN' has to be the same as you said, 162
WinLoss = {'NYY': [[u'L', u'2-6'], [u'L', u'1-3'], [u'W', u'2-1']],
'MIN': [[u'L', u'3-5'], [u'L', u'6-7'], [u'W', u'10-9']],
'CIN': [[u'L', u'0-1'], [u'W', u'1-0'], [u'L', u'6-7']]}

# construct an empty DataFrame here
df = pd.DataFrame()

# just loop through the dictionary you have, and write into DataFrame
# another update to shorten the syntax
df = pd.DataFrame()
for k,v in WinLoss.items():
    # name the columns to whatever you want
    df['{} {}'.format(k, 'Win/Loss')] = [r[0] for r in v]
    df['{} {}'.format(k, 'Scores')] = [r[1] for r in v]

示例结果:

df
  NYY Win/Loss NYY Scores CIN Win/Loss CIN Scores MIN Win/Loss MIN Scores
0            L        2-6            L        0-1            L        3-5
1            L        1-3            W        1-0            L        6-7
2            W        2-1            L        6-7            W       10-9