连接、pandas 和 join_axes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27391081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:45:10  来源:igfitidea点击:

concatenating, pandas, and join_axes

pythonpandas

提问by user3659451

I've been searching the web for an example of this, but haven't found anything.

我一直在网上搜索这样的例子,但没有找到任何东西。

Let df1, df2, .. dfnbe pandas dataframes, indentically indexed.

df1, df2, .. dfn是Pandas数据帧,索引索引。

What is happening when I run the command:

当我运行命令时发生了什么:

pandas.concat([df1,..,dfn],axis=1,join_axes=[df1.index])

pandas.concat([df1,..,dfn],axis=1,join_axes=[df1.index])

It doesn't give me an error and provides a dataframe. I've pasted all I could find in the documentation in relation to this. What happens when there is a mismatch among the indices? How does pandas know to use the indexes of the other dataframes, I thought I might have to put all the indexes of the other n-1pandas dataframes.

它不会给我一个错误并提供一个数据框。我已经粘贴了所有我能在文档中找到的与此相关的内容。当指数之间不匹配时会发生什么?pandas 怎么知道使用其他数据帧的索引,我想我可能必须把其他n-1Pandas数据帧的所有索引。

Any tips?

有小费吗?

join_axes : list of Index objects Specific indexes to use for the other n - 1 axes instead of performing inner/outer set logic

join_axes :索引对象列表 用于其他 n - 1 轴的特定索引,而不是执行内部/外部集合逻辑

回答by j08lue

Meaning of n

的意思 n

First of all, the nin n-1refers to the number of dimensionsin each of the dataframes, not the number of dataframes. You can see that from the source code at lines 938ff:

首先,ninn-1指的是每个数据帧中的数,而不是数据帧的数量。您可以从第 938ff 行的源代码中看到

def _get_new_axes(self):
    ndim = self._get_result_dim()
    new_axes = [None] * ndim

    if self.join_axes is None:
        for i in range(ndim):
            if i == self.axis:
                continue
            new_axes[i] = self._get_comb_axis(i)
    else:
        if len(self.join_axes) != ndim - 1:
            raise AssertionError("length of join_axes must not be "
                                 "equal to {0}".format(ndim - 1))

(Therefore, it should really not read n-1in the documentation. I guess this formulation is based on the common use example where the index passed with join_axesis that of one of the dataframes. The passed index could, though, also be a new, synthetic one.)

(因此,它真的不应该n-1文档中阅读。我猜这个公式是基于常见的使用示例,其中传递的索引join_axes其中一个数据帧的索引。不过,传递的索引也可以是一个新的合成索引.)

Use of join_axes

用于 join_axes

The actual use join_axesis to replacethe indexes of the dataframes that you want to concatenate with a different one (or actually one per dimension).

实际用途join_axes用不同的索引(或实际上每个维度一个)替换要连接的数据帧的索引。

In this process, the values in each dataframe are simply assignedto the new indices, ignoring the index it contains. Furthermore, if one of the dataframes is longer (in any dimension) than the corresponding index, it will simply be truncated.

在这个过程中,每个数据帧中的值被简单地分配给新索引,忽略它包含的索引。此外,如果其中一个数据帧比相应的索引长(在任何维度上),它将被简单地截断。

Merging time series into one dataframe

将时间序列合并为一个数据帧

What you might be trying to achieve is to combine a bunch of Seriesinto a DataFrameand preserve their original (partially) non-matching indices.

您可能想要实现的是将一堆组合Series成 aDataFrame并保留其原始(部分)不匹配的索引。

pandas.concat([df1,..,dfn], axis=1, join='outer')

does that (with join=outer).

这样做(与join=outer)。

(However, when you want to plot the resulting dataframe, you might need to find a workaround, because all columns are interrupted by NaNs.)

(但是,当您想要绘制结果数据框时,您可能需要找到一种解决方法,因为所有列都被 NaNs 中断。)

回答by agcala

Join_axesis deprecated. The supported way is now

不推荐使用Join_axes。现在支持的方式是

pandas.concat([df1,..,dfn],axis=1).reindex(df1.columns)

回答by Subrat Kumar Sahoo

Make join_axes=[df1.columns]:

制作join_axes=[df1.columns]

pandas.concat([df1,..,dfn],axis=1,join_axes=[df1.columns])