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
127 views
in Technique[技术] by (71.8m points)

python - How can we add a field to a data frame, from another data frame, when the sizes/indexes are different?

I'm stumped on this one. I have two data frames that are completely different, but I want to add a field from one to the other. I tried the following.

grouped_and_summed['schedule_code'] = df['schedule_code']

It's giving me all NAN under grouped_and_summed['schedule_code']

I tried pd.concat. That didn't work, because these two data frames have nothing in common. All I want to do is take a value, which has been parsed correctly in df['schedule_code'] and insert it into grouped_and_summed['schedule_code'].

question from:https://stackoverflow.com/questions/65660342/how-can-we-add-a-field-to-a-data-frame-from-another-data-frame-when-the-sizes

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

1 Reply

0 votes
by (71.8m points)

If I got it correctly, concat should do the work if you set correctly the axis:

pd.concat([grouped_and_summed, df['schedule_code']], ignore_index=True, axis=1)

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

...