Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
598 views
in Technique[技术] by (71.8m points)

pandas - Strip an Item to a new column from existing column using python

New to python. I want to strip name from a DF column. I am looping through every item in the list in the column but what's the acceptable way to do this on a large size CSV.

Input DF:

    Entity                                     Details
0  Entity1         street=First,name=John,postcode=123
1  Entity2                                  name=Billy
2  Entity3  street=Second,interest=walking,name=Julian
3  Entity4                                          

Code:

df = pd.DataFrame(
  {
    "Entity": ["Entity1","Entity2","Entity3","Entity4"],
    "Details": ["street=First,name=John,postcode=123",
                "name=Billy",
                "street=Second,interest=walking,name=Julian",
                ""
               ]
  }
)

print(df)

df["Details"] = df["Details"].str.split(',')

lam = (lambda details: list(each_detail for each_detail in details if each_detail.startswith('name') ))

df["Name"] = df["Details"].map(lam)

#Only to change one item list to string, any alternate
df = df.explode("Name")

df["Name"] = df["Name"].str.lstrip('name=')

print(df)

Output DF:

    Entity                                         Details    Name
0  Entity1         [street=First, name=John, postcode=123]    John
1  Entity2                                    [name=Billy]   Billy
2  Entity3  [street=Second, interest=walking, name=Julian]  Julian
3  Entity4                                              []     NaN

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is how to use the regex extract to get the name

df['name'] = df['Details'].str.extract(r'name=(w+)')

this gives

    Entity  Details                                     name
0   Entity1 street=First,name=John,postcode=123         John
1   Entity2 name=Billy                                  Billy
2   Entity3 street=Second,interest=walking,name=Julian  Julian
3   Entity4                                             NaN

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...