Pandas的"concat"方法在连接DataFrame和Series时是否忽略"axi


我创建了一个Series对象。
for index, entry in a_data_frame.iterrows():
...

然后我想将这个系列连接到一个新的/另一个数据帧。我的目标是在对前一个数据帧中的行进行独特重组的基础上构建新的数据帧。

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

该系列将被附加到列的末尾,而不考虑axis参数的值。为什么?

我的实验允许我假设Pandas"认为";系列作为列。当我将一个序列转换为数据帧时,它将产生一个具有单列的帧。

a_series.to_frame()

这对我来说是有意义的,我不能使用(连接(这个系列("列"(和一个数据帧作为"列";行";。最简单的解决方案是在连接之前转换新的数据帧。

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

很难判断代码实际在做什么,但如果您想对Dataframe或Series重新采样,可以使用.sample((方法。

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

输出

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

最新更新