大Pandas从宽到长都带着空数据帧

2024-06-09 13:47:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个非常简单的任务:将wide_to_long应用于一个数据帧,但是每次运行它,我都会得到一个空的数据帧。我几乎可以肯定我的方法是正确的,所以我去了文档并尝试应用那里显示的示例,它还带来了一个空的数据帧!以下是示例代码:

import pandas as pd

df = pd.DataFrame({
    'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    'ht_one': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],
    'ht_two': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
})

l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age', sep='_', suffix='\\w')
l.shape

输出:

^{pr2}$

我试着更新熊猫,但没用。会发生什么事?在


Tags: to数据方法代码文档import示例pandas