简体   繁体   English

如何在 python 中将行值转换为列名和与列值相同的列名?

[英]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".这使得“col1”、“col2”和“col3”的MultiIndex Then the actual columns & values from the value of "col4"然后是“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.我发现这个名字让人们无法调整 dataframe 的表示方式。 Here I remove the name from the column Index so that it can be represented how OP expects在这里,我从列Index中删除了名称,以便可以表示 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM