简体   繁体   English

如何将每个列名和 append 迭代到 python 中的表或数据框

[英]How to iterate for each column name and append it to a table or data frame in python

I want to make a customize correlation table, after a long script this is my final data look like:我想制作一个自定义的相关表,经过一个长脚本后,这是我的最终数据:

ID        a      ...  z                                           
1         76.0   ...  1.033004
2         257.0  ...  0.629641
3         99.0   ...  0.672139
4         52.0   ...  1.007708
5         129.0  ...  1.080922

And this is the script I made to my customize correlation table for a vs all variable这是我为我的自定义相关表制作的脚本,用于 vs 所有变量

pg.pairwise_corr(df_pvt, columns=[['a'], list(df_pvt.columns)], method='pearson')[['X','Y','r']].append
pg.pairwise_corr(df_pvt, columns=[['b'], list(df_pvt.columns)], method='pearson')[['X','Y','r']].append ......... #until variable z

How can I loop or select specific column number without typing variable name 'a' until 'z'?如何在不输入变量名“a”直到“z”的情况下循环或 select 特定列号?

I am new in python.我是 python 的新手。 Thank you in advance先感谢您

import pandas as pd

data = pd.DataFrame({"ID": [1,2,3,4,5], "a": [76.0, 257.0, 99.0,52.0,129.0], "z": [1.033004, 0.629641, 0.672139,1.007708, 1.080922]}, columns=["ID", "a", "z"])

true_columns = []

for index, row in data.iterrows():
    true_col_list = [col for col in data.columns[1] if row[col]]
    true_columns.append(",".join(true_col_list))

data["Columns"] = true_columns

print(data)

As I'm not allowed to comment yet, I think this is what you're looking for.由于我还不能发表评论,我认为这就是你要找的。 Correct me if I'm wrong.如我错了请纠正我。

for letter in 'abcdefghijklmnopqrstuvwxyz':
    pg.pairwise_corr(df_pvt, columns=[letter], list(df_pvt.columns)], method='pearson')[['X','Y','r']].append ....

edit: And I think, if you want to be cool and pythonic (I've seen this in many answers) you could not write out the alphabet yourself but import it with编辑:而且我认为,如果你想变得很酷和 Pythonic(我在很多答案中都看到过),你不能自己写出字母表,而是用

import string
alphabet = string.ascii_lowercase # this equals 'abcdefghijklmnopqrstuvwxyz'

# then iterate over the alphabet
for letter in alphabet:
    # do your stuff here something something[letter] something :P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何迭代数据列的每个单元格,转换并追加每个单元格? - How to iterate each cell of data column,convert and append each cell? 如何将数据框的每一列附加到熊猫系列中? - How to append each column of a data-frame to a series in pandas? 如何根据行/列名称将一个数据框的列附加为另一个数据框的行? - How to append column of one data frame as row of another data frame based on row/column name? 如何迭代行并追加列名 - how to iterate rows and append column name 如何遍历具有固定列的熊猫数据框的每一行并根据python中的条件执行操作? - How can I iterate over each row of a pandas data frame with a fixed column and perform operation on the basis of conditions in python? Python追加到数据框中的特定列 - Python append to specific column in data frame 如何有效地遍历 Python 中选定的 Excel 工作表并将它们附加到数据框中? - How to efficiently iterate through selected Excel sheets in Python and append them into a Data Frame? 如何将 append 到空 pandas 数据框的每一列循环中不同大小的列表? - How to append to each column of empty pandas data frame different size of lists in a loop? Python-如何在列数据框名称pandas中重置数字顺序? - Python - how to reset the order of number in the column data frame name pandas? 如何在python中使用`apply`而不指定数据框的列名? - How to use `apply` in python without specifying the column name of the data frame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM