# Combining two Series into a DataFrame in pandas

1 view

I have two Series s1 and s2 with the same (non-consecutive) indices. How do I combine s1 and s2 to being two columns in a DataFrame and keep one of the indices as a third column?

by (32.5k points)

You can simply use concat and it will give you the desired result:

In : s1 = pd.Series([1, 2], index=['A', 'B'], name='s1')

In : s2 = pd.Series([3, 4], index=['A', 'B'], name='s2')

In : pd.concat([s1, s2], axis=1)

Out:

s1  s2

A   1 3

B   2 4

In : pd.concat([s1, s2], axis=1).reset_index()

Out:

index  s1 s2

0     A 1   3

1     B 2   4

by (107k points)

Pandas will automatically align these passed in series and create the joint index They happen to be the same here. reset_index moves the index to a column.

s1 = Series(randn(5),index=[1,2,4,5,6])

s2 = Series(randn(5),index=[1,2,4,5,6])

DataFrame(dict(s1 = s1, s2 = s2)).reset_index()
{"ft_lang_label":"__label__en","ft_lang_prob":0.6234671,"math_prob":0.9850052,"size":1184,"snap":"2020-10-2020-16","text_gpt3_token_len":474,"char_repetition_ratio":0.13728814,"word_repetition_ratio":0.16216215,"special_character_ratio":0.4518581,"punctuation_ratio":0.22796352,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99356717,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-26T04:08:59Z\",\"WARC-Record-ID\":\"<urn:uuid:9bd0d473-631b-49a8-bfeb-df1be59acf8c>\",\"Content-Length\":\"51726\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e8352d4c-8832-4a89-bec8-e11314f309f3>\",\"WARC-Concurrent-To\":\"<urn:uuid:8a2df2f2-134a-4854-9a0a-f719803b26a5>\",\"WARC-IP-Address\":\"104.20.37.203\",\"WARC-Target-URI\":\"https://intellipaat.com/community/23292/combining-two-series-into-a-dataframe-in-pandas\",\"WARC-Payload-Digest\":\"sha1:QKQA5MTWFFF6LL2C6BIGLXAO4BKTFCC5\",\"WARC-Block-Digest\":\"sha1:643QMMWV5KWVPVQ7XESFB7REYGXT2FDP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146186.62_warc_CC-MAIN-20200226023658-20200226053658-00018.warc.gz\"}"}