Python DataFrame:枢轴行作为列

发布于 2025-01-19 02:47:40 字数 858 浏览 1 评论 0原文

我有来自不同电台的原始文件。当我将它们组合成一个数据框时,我看到三列具有匹配的 id 和名称以及不同的组件。我想将其转换为数据框,其中名称条目成为列名称 代码:

df = 
      id                      name      component
0      1             Serial Number          103
1      2              Station Name           DC
2      1             Serial Number          114
3      2              Station Name           CA
4      1             Serial Number          147
5      2              Station Name           FL

预期答案:

new_df = 
      Station Name      Serial Number     
0     DC                  103
1     CA                  114
2     FL                  147

我的答案:

# Solution1
df.pivot_table('id','name','component')
name 
NaN NaN NaN NaN
# Solution2
df.pivot(index=None,columns='name')['component']
name 
NaN NaN NaN NaN

我没有得到想要的答案。有什么帮助吗?

I have raw files from different stations. When I combine them into a dataframe, I see three columns with matching id and name with different component. I want to convert this into a dataframe where name entries become the column names
Code:

df = 
      id                      name      component
0      1             Serial Number          103
1      2              Station Name           DC
2      1             Serial Number          114
3      2              Station Name           CA
4      1             Serial Number          147
5      2              Station Name           FL

Expected answer:

new_df = 
      Station Name      Serial Number     
0     DC                  103
1     CA                  114
2     FL                  147

My answer:

# Solution1
df.pivot_table('id','name','component')
name 
NaN NaN NaN NaN
# Solution2
df.pivot(index=None,columns='name')['component']
name 
NaN NaN NaN NaN

I am not getting desired answer. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

婴鹅 2025-01-26 02:47:40

首先,您必须使用相同ID的每2行进行每2行,之后您可以使用枢轴表。

import pandas as pd

df = pd.DataFrame({'id': ["1", "2", "1", "2", "1", "2"],
                    'name': ["Serial Number", "Station Name", "Serial Number", "Station Name", "Serial Number", "Station Name"],
                   'component': ["103", "DC", "114", "CA", "147", "FL"]})
new_column = [x//2+1 for x in range(len(df))]
df["id"] = new_column

df = df.pivot(index='id',columns='name')['component']

First you have to make every 2 rows with the same id, after that you can use pivot table.

import pandas as pd

df = pd.DataFrame({'id': ["1", "2", "1", "2", "1", "2"],
                    'name': ["Serial Number", "Station Name", "Serial Number", "Station Name", "Serial Number", "Station Name"],
                   'component': ["103", "DC", "114", "CA", "147", "FL"]})
new_column = [x//2+1 for x in range(len(df))]
df["id"] = new_column

df = df.pivot(index='id',columns='name')['component']
野稚 2025-01-26 02:47:40

如果您的序列号就在站名称之前:

df_ = df.pivot(columns='name', values='component').groupby(df.index // 2).first()
print(df_)

name Serial Number Station Name
0              103           DC
1              114           CA
2              147           FL

If your Serial Number is just before Station Name, you can pivot on name columns then combine the every two rows:

df_ = df.pivot(columns='name', values='component').groupby(df.index // 2).first()
print(df_)

name Serial Number Station Name
0              103           DC
1              114           CA
2              147           FL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文