[英]How to take transpose of one particular DataFrame column in Python? Also how to get certain values from second iteration onwards from 'for' loop?
I am running a 'for' loop whose output is a data frame with two columns, column 1 with columns names and column 2 with data.我正在运行一个“for”循环,其 output 是一个包含两列的数据框,第 1 列包含列名称,第 2 列包含数据。 It can be seen below:如下图所示:
Next, I would take the transpose of this data like this:接下来,我将像这样对这些数据进行转置:
In the next iteration again I would get the similar data(with 2 columns as the first table) for which I don't need columns data, just data should append to the data from the first iteration as seen:在下一次迭代中,我会得到类似的数据(第一个表有 2 列),我不需要列数据,只是数据应该 append 到第一次迭代的数据,如图所示:
Try:尝试:
df = df.append(new_df, ignore_index=True)
For your first question first set your index to you desired column names column, then transpose using T
:对于您的第一个问题,首先将您的索引设置为您想要的列名列,然后使用T
进行转置:
df2=df.set_index('1').T
1 Column1 Column2 Column3 Column4 Column5
2 data_1_1 data_1_2 data_1_3 data_1_4 data_1_5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.