熊猫DF排列单元格并添加前缀

时间:2018-09-13 18:48:44

标签: python pandas dataframe

数据框示例-先前清理过的pandas.read_excel的结果:

import pandas as pd
import numpy as np

raw_data = {'ref': ['409075', '405168', '035751', '039374', '331949', '331951', '307128'], 
        'description': ['Product 7 (12X)', 'Product 6 (8X)', 'Product 2', 'Product 1', 'Product 2', 'Product 3', 'Product 3'], 
        'maker': [np.nan, np.nan, 'Companyname1', 'Comp.2', 'Company3', 'Company name 4', 'Maker 5'],
        'type': [np.nan, np.nan, 'Rev. 0', np.nan, np.nan, np.nan, 'Type 5'],
        'qty': [6, 4, 4, 2, 12, 12, 12],
        'val': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
        'serial': [np.nan, np.nan, np.nan, 58690, 900078, 69402, 900078]}
df = pd.DataFrame(raw_data, columns = ['ref_maker', 'description', 'serial', 'maker', 'type', 'val, ''qty'])

下面是所需的输出(在列中):

  1. ref
  2. a)说明+ b)制造商+ c)类型+ d)val + e)序列
  3. 数量

条件:结果级联的描述列不得超过30个字符,并且不应剪切导入的单元格。

(部分涵盖在Prepare Pandas DataFrame for excel write中)

| ref       | description     (max 30 ch.)  | qty   |
|--------   |----------------------------   |-----  |
| 409075    | Product 7 (12X)               | 6     |
| 405168    | Product 6 (8X)                | 4     |
| 35751     | Product 2                     | 4     |
|           | MKR: Companyname1             |       |
|           | Type: Rev. 0                  |       |
| 39374     | Product 1                     | 2     |
|           | MKR: Comp.2; SRL: 58690       |       |
| 331949    | Product 2                     | 12    |
|           | MKR: Company3; SRL: 900078    |       |
| 331951    | Product 3                     | 12    |
|           | MKR: Company name 4           |       |
|           | SRL: 69402                    |       |
| 307128    | Product 3                     | 12    |
|           | MKR: Maker 5; Type: Type 5    |       |
|           | SRL: 900078                   |       |

我试图找到解决方案,但是由于不了解(正确)解决方案,因此无法成功将其更改为我的需求。希望能满足要求。

感谢您的帮助和提示。

0 个答案:

没有答案