熊猫重新排列数据框

时间:2017-01-04 06:15:29

标签: python pandas

我的数据框如下:

Honda [edit]
Accord (4 models)
Civic  (4 models)
Pilot  (3 models)
Toyota [edit]
Prius  (4 models)
Highlander (3 models)
Ford [edit]
Explorer (2 models)

我希望重塑它,以便得到如下结果的2列数据框:

 Honda     Accord
 Honda     Civic
 Honda     Pilot
 Toyota    Prius
 Toyota    Highlander

等等。 我试过str.split尝试在编辑之间分割,但没有成功。任何建议都非常感谢! Python新手在这里...如果之前已经解决过这么道歉。 谢谢!

到目前为止,我试过

     maker=car['T'].str.extract('(.*\[edit\])', expand=False).str.replace('\[edit\]',"")

这给了我制造商的名单:本田,丰田和福特。然而,我一直在寻找一种方法来提取制造商之间的模型以创建2 col DF。

3 个答案:

答案 0 :(得分:11)

诀窍是首先提取汽车列,然后获取制造商。

import pandas as pd
import numpy as np

df['model'] = df['T'].apply(lambda x: x.split(
    '(')[0].strip() if x.count('(') > 0 else np.NaN)

df['maker'] = df['T'].apply(lambda x: x.split('[')[0].strip(
) if x.count('[') > 0 else np.NaN).fillna(method="ffill")

df = df.dropna().drop('T', axis=1).reindex(
    columns=['maker', 'model']).reset_index(drop=True)

如果条目包含'(',则代码的第一行通过分割和条带字符串操作提取所有汽车,否则分配NaN,我们使用NaN以便我们可以找到制造商后删除这些行。 在此阶段,数据框df将为:

+----+-----------------------+------------+
|    | T                     | model      |
|----+-----------------------+------------|
|  0 | Honda [edit]          | nan        |
|  1 | Accord (4 models)     | Accord     |
|  2 | Civic  (4 models)     | Civic      |
|  3 | Pilot  (3 models)     | Pilot      |
|  4 | Toyota [edit]         | nan        |
|  5 | Prius  (4 models)     | Prius      |
|  6 | Highlander (3 models) | Highlander |
|  7 | Ford [edit]           | nan        |
|  8 | Explorer (2 models)   | Explorer   |
+----+-----------------------+------------+

第二行的内容相同,但对于'['条记录,此处NaNs用于使用fillna填充空的制作单元格 在此阶段,数据框df将为:

+----+-----------------------+------------+---------+
|    | T                     | model      | maker   |
|----+-----------------------+------------+---------|
|  0 | Honda [edit]          | nan        | Honda   |
|  1 | Accord (4 models)     | Accord     | Honda   |
|  2 | Civic  (4 models)     | Civic      | Honda   |
|  3 | Pilot  (3 models)     | Pilot      | Honda   |
|  4 | Toyota [edit]         | nan        | Toyota  |
|  5 | Prius  (4 models)     | Prius      | Toyota  |
|  6 | Highlander (3 models) | Highlander | Toyota  |
|  7 | Ford [edit]           | nan        | Ford    |
|  8 | Explorer (2 models)   | Explorer   | Ford    |
+----+-----------------------+------------+---------+

第三行删除额外记录并重新排列列以及重置索引

|    | maker   | model      |
|----+---------+------------|
|  0 | Honda   | Accord     |
|  1 | Honda   | Civic      |
|  2 | Honda   | Pilot      |
|  3 | Toyota  | Prius      |
|  4 | Toyota  | Highlander |
|  5 | Ford    | Explorer   |

编辑:

更“可爱”的版本(我喜欢一个衬垫)

df = df['T'].str.extractall('(.+)\[|(.+)\(').apply(
    lambda x: x.ffill() 
    if x.name==0 
    else x).dropna(subset=[1]).reset_index(
    drop=True).rename(columns={1:'Model',0:'Maker'})

以上的工作原理如下 extractall会返回一个包含两列的DataFrame;列0对应于使用第一组'(.+)\['提取的正则表达式中的组,即制造商记录以;和列1对应第二组,即'(.+)\('apply用于遍历列,名为0的列将被修改为传播'Maker'通过ffill和列1转发的值将保留原样。然后,dropna与子集1一起使用,以删除列1中的值为NaN的所有行,reset_index用于删除多索引{ {1}}生成。最后,使用rename和对应字典重命名列

enter image description here

另一个班轮(func;))

extractall

答案 1 :(得分:3)

您可以将extractffill一起使用。然后从boolean indexing删除包含[edit]的行,并按str.contains删除,然后reset_index删除创建唯一index的行,最后删除原始列col drop

df['model'] = df.col.str.extract('(.*)\[edit\]', expand=False).ffill()
df['type'] = df.col.str.extract('([A-Za-z]+)', expand=False)
df = df[~df.col.str.contains('\[edit\]')].reset_index(drop=True).drop('col', axis=1)
print (df)
     model        type
0   Honda       Accord
1   Honda        Civic
2   Honda        Pilot
3  Toyota        Prius
4  Toyota   Highlander
5    Ford     Explorer

另一种解决方案使用extractwhere按条件创建新列,并再次使用boolean indexing

df['type'] = df.col.str.extract('([A-Za-z]+)', expand=False)
df['model'] = df['type'].where(df.col.str.contains('\[edit\]')).ffill()
df = df[df.type != df.model].reset_index(drop=True).drop('col', axis=1)
print (df)
         type   model
0      Accord   Honda
1       Civic   Honda
2       Pilot   Honda
3       Prius  Toyota
4  Highlander  Toyota
5    Explorer    Ford

编辑:

如果文本中需要type spaces,请使用replace中的所有值(到最后,还要按s\+删除空格:

print (df)
                             col
0                   Honda [edit]
1              Accord (4 models)
2              Civic  (4 models)
3              Pilot  (3 models)
4                  Toyota [edit]
5              Prius  (4 models)
6          Highlander (3 models)
7                    Ford [edit]
8  Ford Expedition XL (2 models)

df['model'] = df.col.str.extract('(.*)\[edit\]', expand=False).ffill()
df['type'] = df.col.str.replace(r'\s+\(.+$', '')
df = df[~df.col.str.contains('\[edit\]')].reset_index(drop=True).drop('col', axis=1)
print (df)
     model                type
0   Honda               Accord
1   Honda                Civic
2   Honda                Pilot
3  Toyota                Prius
4  Toyota           Highlander
5    Ford   Ford Expedition XL

答案 2 :(得分:1)

尝试
df.set_index(['Col1', 'Col2'])

它会像这样重新排列

COl1 COl2 honda civic civic accord toyota prius highlander

请注意,这是分层数据

相关问题