如何在Python中的数据框中获取元组索引值?

发布于 2025-02-14 01:38:12 字数 979 浏览 1 评论 0 原文

数据框在下面。

 | 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  |

输出应该是像上述数据框架一样的数据框

The data frame is below.

 | 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 |

the tuple of index of data frame:

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

expected data frame;

 | 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  |

the output should be a data frame like above data frame, it should have the index tuple in same row of data frame

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离旧人 2025-02-21 01:38:12

Create 首先按列表理解按3个级别:

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)

然后使用 a>将最后一个级别转换为列 id 带有 a>,最后一个扁平 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  

Create MultiIndex.from_tuples by 3 levels by list comprehension first:

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)

Then use DataFrame.reindex with convert last level to column ID and reshape by DataFrame.unstack with sorting levels by DataFrame.sort_index, last flatten 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  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文