Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - what the advantage of using zip function df['full_name']=zip(df['name'],df['last name'])

i am trying to understand.

what the advantage of using zip function

df['full_name']=zip(df['name'],df['last name'])

instead of

df['full_name']=df['name']+ " " + df['last name']
question from:https://stackoverflow.com/questions/65933866/what-the-advantage-of-using-zip-function-dffull-name-zipdfname-dflast

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

1 Reply

0 votes
by (71.8m points)

In my opinion it's not zip that matters.

df['full_name']=zip(df['name'],df['last name'])

The element in the column full_name are tuples.

df['full_name']=df['name']+ " " + df['last name']

If you write this, the element in the column full_name are strings.

Strings are better for showing and printing, but some information about its structure is lost.

For example, if name is "A" and last name is "B C", if join them with a space, it will be "A B C". It's OK for human, but for machines, it can not tell if it was "A" "B C" or "A B" "C", as "structure info" is lost as I said.

So which one to choose depends on your purpose.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...