python - 在 Python 中取消融化 Pandas 数据框?

标签 python numpy pandas dataform dataframe

我融化了一个 pandas 数据框,用于使用 ggplot 绘图(这通常需要长格式的数据框),如下所示:

test = pandas.melt(iris, id_vars=["Name"], value_vars=["SepalLength", "SepalWidth"])

这会将 iris 数据集的 Name 字段保留在索引中,但会将列 SepalLengthSepalWidth 转换为长格式:

test.ix[0:10]
Out:
           Name     variable  value
0   Iris-setosa  SepalLength    5.1
1   Iris-setosa  SepalLength    4.9
2   Iris-setosa  SepalLength    4.7
3   Iris-setosa  SepalLength    4.6
4   Iris-setosa  SepalLength    5.0
5   Iris-setosa  SepalLength    5.4
6   Iris-setosa  SepalLength    4.6
7   Iris-setosa  SepalLength    5.0
8   Iris-setosa  SepalLength    4.4
9   Iris-setosa  SepalLength    4.9
10  Iris-setosa  SepalLength    5.4

我怎样才能“取消融化”这个数据框?我希望保留 Name 列,但要将 variable 字段的值转换为单独的列。 Name 字段不是唯一的,所以我认为它不能用作索引。我的印象是 pivot 是执行此操作的正确函数,但它是不正确的:

test.pivot(columns="variable", values="value")
KeyError: u'no item named '

我该怎么做?另外,我可以解开有多列长格式的数据帧,即 test 中的多列类似于上面的 variable 列吗?这意味着 columns 似乎必须接受列列表,而不是单个值。谢谢。

最佳答案

我认为这种情况是不明确的,因为 test 数据框没有标识每个唯一行的索引。如果 melt 只是简单地用 value_vars SepalLength 和 SepalWidth 堆叠行,那么您可以手动创建一个索引作为轴心;看起来结果与原始结果相同:

In [15]: test['index'] = range(len(test) / 2) * 2
In [16]: test[:10]
Out[16]: 
          Name     variable  value  index
0  Iris-setosa  SepalLength    5.1      0
1  Iris-setosa  SepalLength    4.9      1
2  Iris-setosa  SepalLength    4.7      2
3  Iris-setosa  SepalLength    4.6      3
4  Iris-setosa  SepalLength    5.0      4
5  Iris-setosa  SepalLength    5.4      5
6  Iris-setosa  SepalLength    4.6      6
7  Iris-setosa  SepalLength    5.0      7
8  Iris-setosa  SepalLength    4.4      8
9  Iris-setosa  SepalLength    4.9      9

In [17]: test[-10:]
Out[17]: 
               Name    variable  value  index
290  Iris-virginica  SepalWidth    3.1    140
291  Iris-virginica  SepalWidth    3.1    141
292  Iris-virginica  SepalWidth    2.7    142
293  Iris-virginica  SepalWidth    3.2    143
294  Iris-virginica  SepalWidth    3.3    144
295  Iris-virginica  SepalWidth    3.0    145
296  Iris-virginica  SepalWidth    2.5    146
297  Iris-virginica  SepalWidth    3.0    147
298  Iris-virginica  SepalWidth    3.4    148
299  Iris-virginica  SepalWidth    3.0    149

In [18]: df = test.pivot(index='index', columns='variable', values='value')
In [19]: df['Name'] = test['Name']
In [20]: df[:10]
Out[20]: 
variable  SepalLength  SepalWidth         Name
index                                         
0                 5.1         3.5  Iris-setosa
1                 4.9         3.0  Iris-setosa
2                 4.7         3.2  Iris-setosa
3                 4.6         3.1  Iris-setosa
4                 5.0         3.6  Iris-setosa
5                 5.4         3.9  Iris-setosa
6                 4.6         3.4  Iris-setosa
7                 5.0         3.4  Iris-setosa
8                 4.4         2.9  Iris-setosa
9                 4.9         3.1  Iris-setosa

In [21]: (iris[["SepalLength", "SepalWidth", "Name"]] == df[["SepalLength", "SepalWidth", "Name"]]).all()
Out[21]: 
SepalLength    True
SepalWidth     True
Name           True

关于python - 在 Python 中取消融化 Pandas 数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15045582/

相关文章:

python - 函数认为我正在传递一个 float

arrays - 垂直和水平连接 numpy 数组

python - numpy 的基本操作是否矢量化,即它们是否使用 SIMD 操作?

python - 如何将 pandas 中数字后面的 N 个索引设为空?

python - Pandas 一次将多列的 datetime64 [ns] 列转换为 datetime64 [ns, UTC]

python - Pandas msgpack vs 泡菜

python - 在 python 中执行列表扩充赋值 (+=) 的动机是什么?

python - ffmpeg的子进程调用返回负值

python - 如何将 Pandas 数据框作为参数传递给 matplotlib 库方法 plot

python - 必须采用 'app_label.ModelName' 形式。"% model ValueError : Invalid model reference