简体   繁体   English

使用dict(python3)重新索引pandas DataFrame

[英]Reindexing a pandas DataFrame using a dict (python3)

Is there a way, without use of loops, to reindex a DataFrame using a dict ? 有没有办法,不使用循环,使用dict重新索引DataFrame Here is an example: 这是一个例子:

df = pd.DataFrame([[1,2], [3,4]])
dic = {0:'first', 1:'second'}

I want to apply something efficient to df for obtaining: 我想对df应用一些有效的东西来获得:

        0  1
first   1  2
second  3  4

Speed is important, as the index in the actual DataFrame I am dealing with has a huge number of unique values. 速度很重要,因为我正在处理的实际DataFrame的索引具有大量的唯一值。 Thanks 谢谢

You need the rename function: 您需要重命名功能:

df.rename(index=dic)

#       0   1
#first  1   2
#second 3   4

Modified the dic to get the results: dic = {0:'first', 1:'second'} 修改了dic来得到结果: dic = {0:'first', 1:'second'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM