chevron_leftCreating DataFrames Cookbook
Combining multiple Series into a DataFrameCombining multiple Series to form a DataFrameConverting a Series to a DataFrameConverting list of lists into DataFrameConverting list to DataFrameConverting percent string into a numeric for read_csvConverting scikit-learn dataset to Pandas DataFrameConverting string data into a DataFrameCreating a DataFrame from a stringCreating a DataFrame using listsCreating a DataFrame with different type for each columnCreating a DataFrame with empty valuesCreating a DataFrame with missing valuesCreating a DataFrame with random numbersCreating a DataFrame with zerosCreating a MultiIndex DataFrameCreating a Pandas DataFrameCreating a single DataFrame from multiple filesCreating empty DataFrame with only column labelsFilling missing values when using read_csvImporting DatasetImporting tables from PostgreSQL as Pandas DataFramesInitialising a DataFrame using a constantInitialising a DataFrame using a dictionaryInitialising a DataFrame using a list of dictionariesInserting lists into a DataFrame cellKeeping leading zeroes when using read_csvParsing dates when using read_csvPreventing strings from getting parsed as NaN for read_csvReading data from GitHubReading file without headerReading large CSV files in chunksReading n random lines using read_csvReading space-delimited filesReading specific columns from fileReading tab-delimited filesReading the first few lines of a file to create DataFrameReading the last n lines of a fileReading URL using read_csvReading zipped csv file as a DataFrameRemoving Unnamed:0 columnResolving ParserError: Error tokenizing dataSaving DataFrame as zipped csvSkipping rows without skipping header for read_csvSpecifying data type for read_csvTreating missing values as empty strings rather than NaN for read_csv
check_circleMark as learned
Combining multiple Series to form a DataFrame in Pandas
schedule Mar 5, 2023Last updated
tocTable of Contentsexpand_more
Check out the interactive map of data science
To combine multiple Series to form a DataFrame in Pandas, use the
Horizontally stacking Series
To horizontally stack two Series:
s1 = pd.Series(['a','b'])s2 = pd.Series(['c','d'])df = pd.concat([s1, s2], axis=1) # returns a DataFramedf0 10 a c1 b d
s2 share the same index (
[0,1]) so they could be stacked horizontally (
Case when index labels do not align
In the case when the index labels don't perfectly align, we would end up with missing values:
s1 = pd.Series(['a','b'], index=["A","B"])s2 = pd.Series(['c','d'], index=["B","C"])pd.concat([s1, s2], axis=1)0 1A a NaNB b cC NaN d
Supplement - assigning new column labels
The resulting DataFrame will have the default integer indices as the column labels, so you may want to assign new labels like so:
df.columns = ["A","B"]dfA Ba a NaNb b cc NaN d
Vertically stacking Series
To vertically stack multiple Series to form a new DataFrame, use
s1 = pd.Series(['a','b'])s2 = pd.Series(['c','d'])df = pd.concat([s1, s2], axis=0).to_frame()df00 a1 b0 c1 d
Note the following:
concat(~)means that we want to stack the Series vertically, as opposed to horizontally.
the return type of
concat(~)in this case is a Series, and so to convert this Series into a DataFrame, we use
Pandas | concat method
Concatenates a list of Series or DataFrame, either horizontally or vertically.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
Enjoy our search
Hit / to insta-search docs and recipes!