亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PANDAS:刪除索引值并在 Excel 中將一個單元格中的值拆分為不同的行

PANDAS:刪除索引值并在 Excel 中將一個單元格中的值拆分為不同的行

弒天下 2022-11-01 14:00:18
我編寫了這段代碼來從一個 excel 文件中提取數據,并再次準備另一個帶有提取的 ID、版本、階段的 excel 文件。它提取它但錯誤地放置在excel中。它將值與索引一起放入excel中所有ID在一個單元格中所有版本在一個單元格中所以我希望它位于Id列下方的不同行這是我的代碼[輸入文件鏈接](https://drive .google.com/file/d/1ZrUbftMppFf8L3jgWl2i8bsgUavOHnkZ/view?usp=sharing ) [輸出文件鏈接] ( https://drive.google.com/file/d/1BoUiICzRgkX3AN9OcRIEDnh9c3qKNL3l/view?usp=sharing )import pandas as pdcols = ['ID']vals = ['Version']phas = ['Phase']id_index_list = []id_list = []verindex_list = []version_list = []phaseindex_list = []phases_list = []tolist = []df_pver = pd.read_excel('pver.xlsm', 'PVP', header = None)#Name of Projectdz= df_pver.iloc[[0],[0]]#Finding IDdy= df_pver.xs(0)for id in dy:    if 'ID' in str(id):        #ID list Finding location of Column where ID exists        zr= dy[dy==id].index.values        for item in zr:            if not item in id_index_list:                id_index_list.append(item)        mylist = [df_pver.xs(0)[id_index_list]]        #finding the location of ID and moving 3 column aside to find the Version        ze= dy[dy==id].index.values + 3        for item in ze:            if not item in verindex_list:                verindex_list.append(item)        mylist2 = [df_pver.xs(0)[verindex_list]]        #findind the phase the project is in        zp= dy[dy==id].index.values + 1        for item in zp:            if not item in phaseindex_list:                phaseindex_list.append(item)        mylist3 = [df_pver.xs(1)[phaseindex_list]]id_list.append(mylist)version_list.append(mylist2)phases_list.append(mylist3)a = pd.DataFrame(id_list, columns = cols)b = pd.DataFrame(version_list, columns = vals)c = pd.DataFrame(phases_list, columns = phas)此代碼正在查找我需要但錯誤地給出輸出的數據。excel中的輸出是   id                  version                     required  0  17 X 18 Y 22 Z     20 1  21 2 24 3            18 gantt 19 Pie 23 ipex但我希望它像這樣,沒有索引也分成不同的行,而不是全部在一個單元格中。  id      version     required0 X        1           gantt1 Y        2           Pie2 Z        3           ipex有人可以幫忙嗎
查看完整描述

1 回答

?
婷婷同學_

TA貢獻1844條經驗 獲得超8個贊

我假設您的列已經按 id、階段和版本的順序排列。索引 3 之后也沒有數字ID:


# read excel

df = pd.read_excel('pver.xlsx')

# find the columns that start with ID, transpose, reset the index and split on colon

ids = df[df.columns[df.columns.str.startswith('ID')]].T.reset_index()['index'].str.split(':.|:', expand=True)[1].to_numpy()

# find the columns that start with QA and transpose

phase = df[df.columns[df.columns.str.startswith('QA')]].T[0].to_numpy()

# find the columns that start with V or OEM, and transpose

v = df[df.columns[df.columns.str.startswith('V') | df.columns.str.startswith('OEM SW')]].T.index

# vstack and to pd.dataFrame

new_df = pd.DataFrame(np.vstack([ids,v,phase])).T

# name columns

new_df.columns = ['ID', 'Version', 'Phase']


           ID        Version               Phase

0     1907839           V100  during development

1    01907820           V110  during development

2   189634226           V120  during development

3                       V130  during development

4           1           V200       Raw Container

5           2           V220                 NaN

6           3    OEM SW name                 NaN

7           4  OEM SW name.1                 NaN

8           5  OEM SW name.2                 NaN

9           6  OEM SW name.3                 NaN

10          7  OEM SW name.4                 NaN

11          8  OEM SW name.5                 NaN

12          9  OEM SW name.6                 NaN

13         10  OEM SW name.7                 NaN

14         11  OEM SW name.8                 NaN

15         12  OEM SW name.9                 NaN


查看完整回答
反對 回復 2022-11-01
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號