将列值更改为pandas中的列标题 [英] Change column values to column headers in pandas
本文介绍了将列值更改为pandas中的列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,该代码将pandas数据框的一列中的值用作新数据框的列.数据框第一列中的值成为新数据框的索引.
I have the following code, which takes the values in one column of a pandas dataframe and makes them the columns of a new data frame. The values in the first column of the dataframe become the index of the new dataframe.
从某种意义上说,我想将邻接列表转换成邻接矩阵.到目前为止,这是代码:
In a sense, I want to turn an adjacency list into an adjacency matrix. Here's the code so far:
import pandas as pa
print "Original Data Frame"
# Create a dataframe
oldcols = {'col1':['a','a','b','b'], 'col2':['c','d','c','d'], 'col3':[1,2,3,4]}
a = pa.DataFrame(oldcols)
print a
# The columns of the new data frame will be the values in col2 of the original
newcols = list(set(oldcols['col2']))
rows = list(set(oldcols['col1']))
# Create the new data matrix
data = np.zeros((len(rows), len(newcols)))
# Iterate over each row and fill in the new matrix
for row in zip(a['col1'], a['col2'], a['col3']):
rowindex = rows.index(row[0])
colindex = newcols.index(row[1])
data[rowindex][colindex] = row[2]
newf = pa.DataFrame(data)
newf.columns = newcols
newf.index = rows
print "New data frame"
print newf
这适用于此特定实例:
Original Data Frame
col1 col2 col3
0 a c 1
1 a d 2
2 b c 3
3 b d 4
New data frame
c d
a 1 2
b 3 4
如果col3中的值不是数字,它将失败.我的问题是,这样做是否有更优雅/更稳健的方式?
It will fail if the values in col3 are not numbers. My question is, is there a more elegant/robust way of doing this?
推荐答案
这看起来像收益
col3
col2 c d
col1
a 1 2
b 3 4
如果您不希望使用MultiIndex列,则可以使用以下命令删除col3
:
If you don't want a MultiIndex column, you can drop the col3
using:
newf.columns = newf.columns.droplevel(0)
然后会产生
col2 c d
col1
a 1 2
b 3 4
这篇关于将列值更改为pandas中的列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文