简体   繁体   English

如何对 Python 中的一个特定 DataFrame 列进行转置? 另外如何从'for'循环的第二次迭代中获取某些值?

[英]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.

相关问题 如何用第二个 DataFrame 中的值替换一个 DataFrame 中的列中的值,两者在 Python Pandas 中都有主键? - How to replace values in column in one DataFrame by values from second DataFrame both have major key in Python Pandas? 如何从python pandas dataframe的特定列中检查缺失值? - How to check missing values from particular column of python pandas dataframe? Python如何获取一个数据帧中的值,而不是第二数据帧中的值 - Python How to get values that are in one dataframe but not a second python dataframe:如何转置不同的列值 - python dataframe : How to transpose distinct column values 如何从特定范围的单元格中获取值并将其复制到 Python 中的 dataframe 中的特定范围内? - How can I take values from a certain range of cells and copy it to a certain range in the dataframe in Python? 如何从一个 pandas dataframe 获取行值并将它们用作从另一个 dataframe 获取值的参考 - How to take row values from one pandas dataframe and use them as reference to get values from another dataframe 如何获取值以及具有该特定值的行数,该值是从pandas数据帧上的多个条件得出的? - How to get values and also number of rows having that particular value, which is derived from multiple condtions on pandas dataframe? 如何通过 python 中的循环从 dataframe 中一一删除列? - How to remove column one by one from a dataframe through a loop in python? 如何从一列中查找也出现在 Python 中 DataFrame 的另一列中的元素 - How to find the elements from one column which also appear in another column of a DataFrame in Python 如何从 Python 的 for 循环中的列表中获取某些值? - How do I get certain values from a list in a for loop in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM