简体   繁体   中英

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:

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:

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:

        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'] .

When you run {element: re.sub(' Equity', '', element) for element in old.index} (a dictionary comprehension), you will get:

{'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.

Note that neither tolist() not parentheses surrounding re.sub(...) are not needed (the result is the same).

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.

That's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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