pandas DataFrame 生成行

pandas dataframe generate rows

提问人:Romeo Gherasim 提问时间:4/23/2023 更新时间:4/23/2023 访问量:42

问:

我有一个输入数据帧:

import pandas as pd

# Define the input data
data = {
    'ID':  [500, 200, 300],
    'A': [3, 3, ''],
    'B': [3, 1, ''],
    'C': [2, '' ,''],
    'D': ['', 2, 1],
    'E': ['', '',2 ],
}

# Convert the input data to a Pandas DataFrame
df = pd.DataFrame(data)

输入表

enter image description here

我需要转换此输入,如下面的输出示例所示:

enter image description here

如果您有任何想法,请分享。谢谢!

Pandas DataFrame 数据操作

评论


答:

1赞 mozway 4/23/2023 #1

您可以使用每行的最大数量重复这些行,然后与 groupby.cumcount 进行比较以映射 /:1''

tmp = df.set_index('ID').apply(pd.to_numeric, errors='coerce')

out = (
 tmp.loc[tmp.index.repeat(tmp.max(axis=1))]
    .pipe(lambda d: d.gt(d.groupby(level='ID').cumcount(), axis=0))
    .replace({True: 1, False: ''})
    .reset_index()
)

铌。如果使用 NaN 代替空字符串,则可以简化代码。

输出:

    ID  A  B  C  D  E
0  500  1  1  1      
1  500  1  1  1      
2  500  1  1         
3  200  1  1     1   
4  200  1        1   
5  200  1            
6  300           1  1
7  300              1