熊猫 - 枢轴/堆栈/unstack/熔体

发布于 2025-01-31 08:13:12 字数 2948 浏览 3 评论 0原文

我有一个看起来像这样的数据框:

名称值1值2
A100101
A100102
A100103
B200201
b200 B 200202
B200203
C300301 C 300 301
C300302
C300303

,我正在尝试实现此处:

名称值1值2值3值4值5值6
A100101100102100103
B200201200202200203
C300301 300302300300303

这是我到目前为止尝试的; dataframe.stack() dataframe.unstack() dataframe.melt(id_vars = ['name'])

我需要通过确保;

  1. 第一行保持原样,但是与同名相关的每个后续值都应将其转换为coulmn。
  2. 第二个值b(对于。ex)应将其关联的值作为新值下的新值转置,但它不应完全形成单独的值。

I have a dataframe that looks like this:

namevalue 1value 2
A100101
A100102
A100103
B200201
B200202
B200203
C300301
C300302
C300303

And I'm trying to get to this:

namevalue 1value 2value 3value 4value 5value 6
A100101100102100103
B200201200202200203
C300301300302300303

Here is what i have tried so far;
dataframe.stack()
dataframe.unstack()
dataframe.melt(id_vars=['name'])

I need to transpose the data by ensuring that;

  1. The first row remains as it is but every subsequent value associated with the same name should be transposed to a coulmn.
  2. Whereas the second value B (for. ex) should transpose it's associated value as a new value under the column A values, it should not form a separate altogether.

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

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

发布评论

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

评论(3

触ぅ动初心 2025-02-07 08:13:12

尝试:

def fn(x):
    vals = x.values.ravel()
    return pd.DataFrame(
        [vals],
        columns=[f"value {i}" for i in range(1, vals.shape[0] + 1)],
    )


out = (
    df.set_index("name")
    .groupby(level=0)
    .apply(fn)
    .reset_index()
    .drop(columns="level_1")
)
print(out.to_markdown())

打印:

名称值1值2值3值4值5值6
0A100101100102100103
1B200201200202200203
2C300301300 302300300303

Try:

def fn(x):
    vals = x.values.ravel()
    return pd.DataFrame(
        [vals],
        columns=[f"value {i}" for i in range(1, vals.shape[0] + 1)],
    )


out = (
    df.set_index("name")
    .groupby(level=0)
    .apply(fn)
    .reset_index()
    .drop(columns="level_1")
)
print(out.to_markdown())

Prints:

namevalue 1value 2value 3value 4value 5value 6
0A100101100102100103
1B200201200202200203
2C300301300302300303
够钟 2025-02-07 08:13:12

每个名称的平坦值

(
    df.set_index('name')
    .groupby(level=0)
    .apply(lambda x: pd.Series(x.values.flat))
    .rename(columns=lambda x: f'value {x + 1}')
    .reset_index()
)

Flatten values for each name

(
    df.set_index('name')
    .groupby(level=0)
    .apply(lambda x: pd.Series(x.values.flat))
    .rename(columns=lambda x: f'value {x + 1}')
    .reset_index()
)
顾挽 2025-02-07 08:13:12

使用融化, groupby`和 pivot_wider (来自 pyjanitor ):

# pip install pyjanitor
import pandas as pd
import janitor

(df
.melt('name', ignore_index = False)
.sort_index()
.drop(columns='variable')
.assign(header = lambda df: df.groupby('name').cumcount() + 1)
.pivot_wider('name', 'header', names_sep = ' ')
)
  name  value 1  value 2  value 3  value 4  value 5  value 6
0    A      100      101      100      102      100      103
1    B      200      201      200      202      200      203
2    C      300      301      300      302      300      303

One option using melt,groupby`, and pivot_wider (from pyjanitor):

# pip install pyjanitor
import pandas as pd
import janitor

(df
.melt('name', ignore_index = False)
.sort_index()
.drop(columns='variable')
.assign(header = lambda df: df.groupby('name').cumcount() + 1)
.pivot_wider('name', 'header', names_sep = ' ')
)
  name  value 1  value 2  value 3  value 4  value 5  value 6
0    A      100      101      100      102      100      103
1    B      200      201      200      202      200      203
2    C      300      301      300      302      300      303
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文