[英]How to convert row values as column names and same column names as column values in python?
I want to convert row values as column names and those column names would be the column values.我想将行值转换为列名,这些列名将是列值。 I have tried with pivot but it is not giving desired output.
我已经尝试过使用 pivot,但它没有提供所需的 output。
Data:
col1 col2 col3 col4
x1 y1 z1 a1
x1 y1 z1 a2
x1 y1 z1 a3
I have tried like below:我试过如下:
Data.pivot(columns='Col4', values='col4')
Output:
a1 a2 a3
a1 NAN NAN
NAN a2 NAN
NAN NAN a3
Desired output:
col1 col2 col3 a1 a2 a3
x1 y1 z1 a1 a2 a3
You can use pivot
, but you need to specify the index
argument to keep your "col1", "col2", "col3".您可以使用
pivot
,但您需要指定index
参数以保留“col1”、“col2”、“col3”。 Once you do that, you can clean up the dataframe a little bit to get the result you want.一旦你这样做了,你可以稍微清理一下 dataframe 以获得你想要的结果。
out = (df.pivot(index=["col1", "col2", "col3"], columns="col4", values="col4")
.rename_axis(columns=None)
.reset_index())
print(out)
col1 col2 col3 a1 a2 a3
0 x1 y1 z1 a1 a2 a3
Steps脚步
pivot(...)
: pivot the dataframe as desired. pivot(...)
: pivot dataframe 根据需要。 This makes a MultiIndex
of "col1", "col2", and "col3".MultiIndex
。 Then the actual columns & values from the value of "col4"rename_axis(columns=None)
: pivot
makes a columns an Index
object with a name. rename_axis(columns=None)
: pivot
使列成为具有名称的Index
object。 I find that this name throws people off be adjusting how the dataframe is represented.Index
so that it can be represented how OP expectsIndex
中删除了名称,以便可以表示 OP 的期望reset_index()
as stated earlier- pivot
makes a MultiIndex
of "col1", "col2", and "col3".reset_index()
- pivot
生成“col1”、“col2”和“col3”的MultiIndex
。 We use reset_index()
here to take these values and insert it into the actual data of the DataFrame
instead of being a MultiIndex
reset_index()
来获取这些值并将其插入到DataFrame
的实际数据中,而不是作为MultiIndex
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.