簡體   English   中英

如何在python的數據框中獲取元組索引值?

[英]how to get tuple index values in data frame in python?

數據框如下。

 | ID | FIRST_NAME| LAST_NAME| MOBILE_NUMBER | DIRECT_NUMBER|
 | ---|-----------------------------------------------------|
 | 1  |  Richard | dietzen  | +18708007709  | not available |
 | 2  |  William |macdonald | not available | +15611784776  |
 | 3  |  Richard | Dietzen  | +18708007709  | not available |
 | 4  |  dale    | Sowders  | +16162900340  | not available |
 | 5  |  dale    | Sowders  | +18708007709  | not available |

數據框索引的元組:

|(1, 3)|
|(4, 5)|

預期的數據框;

 | ID_1 | FIRST_NAME_1 |...... | DIRECT_NUMBER_1| ID_2 | FIRST_NAME_2|......| DIRECT_NUMBER_2|
 | ---  |------------------------------------------------------------------------------------|
 | 1    |  richard     | ......| not available  |  3   |   richard   |......| not available  |
 | 4    |  dale        | ......| not available  |  5   |   dale      |......| not available  |

輸出應該是像上面的數據框一樣的數據框,它應該在數據框的同一行中有索引元組

首先通過列表理解創建 3 個級別的MultiIndex.from_tuples

tups = [(1,3),(4,5),(3,4)]

#if necessary set ID to index
df  = df.set_index('ID')

L = [(a,i+1, x) for a, b in enumerate(tups) for i, x in enumerate(b) ]
mux = pd.MultiIndex.from_tuples(L)

然后使用DataFrame.reindex將最后一個級別轉換為列ID並通過DataFrame.sort_index重塑,通過DataFrame.unstack排序級別,最后展平MultiIndex

df = (df.reindex(mux, level=2)
        .reset_index(level=-1)
        .rename(columns={'level_2':'ID'})
        .unstack()
        .sort_index(axis=1, level=1, sort_remaining=False))

df.columns = df.columns.map(lambda x: f'{x[0]}_{x[1]}')
print (df)
   ID_1 FIRST_NAME_1 LAST_NAME_1 MOBILE_NUMBER_1 DIRECT_NUMBER_1  ID_2  \
0     1      Richard     dietzen    +18708007709   not available     3   
1     4         dale     Sowders    +16162900340   not available     5   
2     3      Richard     Dietzen    +18708007709   not available     4   

  FIRST_NAME_2 LAST_NAME_2 MOBILE_NUMBER_2 DIRECT_NUMBER_2  
0      Richard     Dietzen    +18708007709   not available  
1         dale     Sowders    +18708007709   not available  
2         dale     Sowders    +16162900340   not available  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM