简体   繁体   English

Python:.Rename & index - 这是做什么的?

[英]Python: .Rename & index - What is this doing?

Python 新手,尝试找出以下行在 Python 中所做的任何帮助将不胜感激

new = old.rename(index={element: (re.sub(' Equity', '', element)) for element in old.index.tolist()})

Assuming old is pandas DataFrame, the code is renaming the index (see rename ) by removing the word Equity from each of the strings on it, for example:假设 old 是 pandas DataFrame,代码通过从其上的每个字符串中删除单词 Equity 来重命名索引(请参阅rename ),例如:

import pandas as pd
import re

old = pd.DataFrame(list(enumerate(['Some Equity', 'No Equity', 'foo', 'foobar'])), columns=['id', 'equity'])
old = old.set_index('equity')

print(old)

Output (Before)输出(之前)

             id
equity         
Some Equity   0
No Equity     1
foo           2
foobar        3

Then if you run:然后如果你运行:

new = old.rename(index={element: (re.sub(' Equity', '', element)) for element in old.index.tolist()})

Output (After)输出(之后)

        id
equity    
Some     0
No       1
foo      2
foobar   3

The following expression, is known as dictionary comprehension :以下表达式称为字典理解

{element: (re.sub(' Equity', '', element)) for element in old.index.tolist()}

for the data of the example above creates the following dictionary :对于上面示例的数据,创建了以下字典

{'Some Equity': 'Some', 'No Equity': 'No', 'foo': 'foo', 'foobar': 'foobar'}

Assume that source CSV file has the following content:假设源 CSV 文件具有以下内容:

c1,c2
Abc,A,10
 Equity,B,20
Cex,C,30
Dim,D,40

If you run如果你跑

old = pd.read_csv('input.csv', index_col=[0])

then old will have the following content:那么old将具有以下内容:

        c1  c2
Abc      A  10
 Equity  B  20
Cex      C  30
Dim      D  40

Let's look at each part of your code.让我们看看代码的每个部分。

old.index.tolist() contains: ['Abc', ' Equity', 'Cex', 'Dim'] . old.index.tolist()包含: ['Abc', ' Equity', 'Cex', 'Dim']

When you run {element: re.sub(' Equity', '', element) for element in old.index} (a dictionary comprehension), you will get:当您运行{element: re.sub(' Equity', '', element) for element in old.index} (字典理解)时,您将得到:

{'Abc': 'Abc', ' Equity': '', 'Cex': 'Cex', 'Dim': 'Dim'}

so each value is equal to its key with one exception: The value for ' Equity' key is an empty string.所以每个值都等于它的键,但有一个例外: ' Equity'键的值是一个空字符串。

Note that neither tolist() not parentheses surrounding re.sub(...) are not needed (the result is the same).请注意,不需要围绕re.sub(...) 的tolist() 和括号(结果相同)。

And the last step:最后一步:

new = old.rename(index=...) changes the index in old , substituting ' Equity' with an empty string and the result is saved under new variable. new = old.rename(index=...)更改old 中的索引,将' Equity'替换为空字符串,结果保存在new变量下。

That's all.就这样。

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

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