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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…