英文:
MultiIndex names when using pd.concat disappeared
问题
考虑到你的要求,以下是翻译好的部分:
考虑以下数据框 `df1` 和 `df2`:
df1:
sim_names Model 1
signal_names my_y1 my_y2
units °C kPa
(Time, s)
0.0 0.738280 1.478617
0.1 1.078653 0.486527
0.2 0.794123 0.604792
0.3 0.392690 1.072772
df2:
空的数据框
列: []
索引: [0.0, 0.1, 0.2, 0.3]
正如你所见,`df1` 有三个级别的名称分别为 `"sim_names"`、`"signal_names"` 和 `"units"`。
接下来,我想要将这两个数据框连接起来,因此我运行了以下命令:
df2 = pd.concat(
[df1, df2],
axis="columns",
)
但是我得到了以下结果:
df2:
Model 1
my_y1 my_y2
°C kPa
(Time, s)
0.0 0.738280 1.478617
0.1 1.078653 0.486527
0.2 0.794123 0.604792
0.3 0.392690 1.072772
正如你所见,级别名称消失了。
我应该怎么做才能在结果的 `df2` 中保留 `df1` 的级别名称?
我想要的结果 `df2` 应该像下面这样:
df2:
sim_names Model 1
signal_names my_y1 my_y2
units °C kPa
(Time, s)
0.0 0.738280 1.478617
0.1 1.078653 0.486527
0.2 0.794123 0.604792
0.3 0.392690 1.072772
我尝试将 `names=["sim_names", "signal_names", "units"]` 作为参数传递给 `pd.concat`,但是得到了与上述相同的错误结果。
英文:
Consider the following dataframes df1
and df2
:
df1:
sim_names Model 1
signal_names my_y1 my_y2
units °C kPa
(Time, s)
0.0 0.738280 1.478617
0.1 1.078653 0.486527
0.2 0.794123 0.604792
0.3 0.392690 1.072772
df2:
Empty DataFrame
Columns: []
Index: [0.0, 0.1, 0.2, 0.3]
As you see, df1
has three levels with names "sim_names", "signal_names" and "units"
.
Next, I want to concatenate the two dataframes, and therefore I run the following command:
df2 = pd.concat(
[df1, df2],
axis="columns",
)
but what I get is the following:
df2:
Model 1
my_y1 my_y2
°C kPa
(Time, s)
0.0 0.738280 1.478617
0.1 1.078653 0.486527
0.2 0.794123 0.604792
0.3 0.392690 1.072772
As you see, the levels names are gone.
What should I do to keep the levels names of df1
in the resulting df2
?
My wanted resulting df2
should be like the following:
df2:
sim_names Model 1
signal_names my_y1 my_y2
units °C kPa
(Time, s)
0.0 0.738280 1.478617
0.1 1.078653 0.486527
0.2 0.794123 0.604792
0.3 0.392690 1.072772
I tried to pass names=["sim_names", "signal_names", "units"]
as argument to pd.concat
but I got the same wrong result as above.
答案1
得分: 1
I'm not sure but seems like this is the normal behaviour (see GH13475).
作为一种解决方法,您可以使用 rename_axis
/names
:
out = pd.concat(
[df1, df2],
axis="columns",
).rename_axis(df1.columns.names, axis=1) # <- added chain
Output :
print(out)
sim_names Model 1
signal_names my_y1 my_y2
units ℃ kPa
(Time, s)
0.00 0.74 1.48
0.10 1.08 0.49
0.20 0.79 0.60
0.30 0.39 1.07
英文:
I'm not sure but seems like this is the normal behaviour (see GH13475).
As a workaround, you can use rename_axis
/names
:
out = pd.concat(
[df1, df2],
axis="columns",
).rename_axis(df1.columns.names, axis=1) # <- added chain
Output :
print(out)
sim_names Model 1
signal_names my_y1 my_y2
units °C kPa
(Time, s)
0.00 0.74 1.48
0.10 1.08 0.49
0.20 0.79 0.60
0.30 0.39 1.07
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论