0

I created a Series object.

for index, entry in a_data_frame.iterrows():
    ...

Then I would like to concatenate this series to a new/another data frame. My goal is to build up the new data frame based on some unique recombination of the rows in the previous one.

a_new_frame = pandas.concat((a_new_frame, a_series))

The series will be appended to the end of the columns disregarding the value of the axis parameter. Why?

2 Answers 2

1

My experiments allow me to assume that Pandas "thinks" series as columns. When I convert a series to a data frame, it will result in a frame with a single column.

a_series.to_frame()

It makes sense to me that I was unable to use (to concatenate) this series ("column") with a data frame as a "row". The simplest solution is to transpose the new data frame before concatenation.

a_series.to_frame().transpose()
a_new_frame = pandas.concat((a_new_frame, a_series.to_frame().transpose()))
Sign up to request clarification or add additional context in comments.

Comments

0

It's hard to tell what your code is actually doing, but if you want to resample a Dataframe or a Series you can use the .sample() method.

series = pd.Series([1,2,3,4,5])
series.sample(len(series))

Output

2    2
0    1
4    2
1    1
3    2
Name: Path Id, dtype: object

4 Comments

Thanks for the tip. But no. In a nutshell, I store activities in the data frame, like doing something during my workday and so on. I have to merge subsequent activities of the same type. My time tracking system breaks up these into several instances. But I thought it is irrelevant for the problem mentioned in the question, building up a frame series by series.
It would be useful to post the expected input and output you're trying to get. Its hard to tell what the variables are actually representing in the sample code you posted.
Thanks for the remark. I changed the question, but I made it more general because it is independent of the actual problem. The lesson is that pandas "thinks" series as columns by default, not as rows, as I naively thought. I hope I will spare some time for the next one who tries to build a frame similarly.
It’s not a default. Series are always used to represent columns in pandas. Any column in a Dataframe is a Series. Glad you managed to figure out a working solution though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.