If I have a DataFrame
:
students = pd.DataFrame([
['Alex'],
['Lauren'],
])
How can I concatenate a Series
and create a new DataFrame
? For example, I'd like:
>>> marks = pd.Series([.8, .75])
>>> students.concat(marks).values
[['Alex', .8],
['Lauren', .75]]
I know that I could use:
students['marks'] = marks
But that would mutate students
.
I've tried:
>>> pd.concat([students, marks])
…
AttributeError: 'Series' object has no attribute '_data'
append() to Combine Two Series. You can use pandas. DataFrame(Series. append(Series,ignore_index=True)) to create a DataFrame by appending series to another series.
To combine multiple Series into a single DataFrame, use the concat(~) method or the DataFrame(~) constructor.
Pandas DataFrame. append() will append rows (add rows) of other DataFrame, Series, Dictionary or list of these to another DataFrame.
You can convert to DataFrame and concatenate afterwards:
>>> pd.concat([students, pd.DataFrame(marks)], axis=1)
0 0
0 Alex 0.80
1 Lauren 0.75
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With