Home > OS >  Does Pandas `concat` method ignore the `axis` parameter, when concatenating a DataFrame with a Serie
Does Pandas `concat` method ignore the `axis` parameter, when concatenating a DataFrame with a Serie

Time:02-17

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.

concatenated_data = pandas.concat((concatenated_data, concat_entry))

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

CodePudding user response:

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.

concat_entry.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.

concat_entry.to_frame().transpose()
concatenated_data = pandas.concat((concatenated_data, concat_entry.to_frame().transpose()))

CodePudding user response:

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
  • Related