简体   繁体   English

如何在python中的单元格值旁边添加标题名称

[英]How to add a header name next to its cell value in python

I have this table as an input and I would like to add the name of the header to its corresponding cells before converting it to a dataframe 我将此表作为输入,我想在将其转换为数据框之前将标题的名称添加到其对应的单元格中

在此处输入图片说明

I am generating association rules after converting the table to a dataframe and each rule is not clear if it belongs to which antecedent/consequent. 我将表转换为数据框后生成关联规则,并且不清楚每个规则是否属于哪个先行/后继。

Example for the first column of my desired table: 我所需表的第一列的示例:

Age
Age = 45
Age = 30 
Age = 45
Age = 80 

. . and so on for the rest of the columns. 对于其余的列,依此类推。 What is the best way to access each column and rewrite them? 访问各列并重写它们的最佳方法是什么? And is there a better solution to reference my values after generating association rules other than adding the name of the header to each cell? 除了在每个单元格中添加标头的名称之外,还有更好的解决方案在生成关联规则之后引用我的值吗?

Here is one way to add the column names to all cells: 这是将列名称添加到所有单元格的一种方法:

df = pd.DataFrame({'age':[1,2],'sex':['M','F']})
df = df.applymap(str)

for c in df.columns:
    df[c] = df[c].apply(lambda s: "{} = {}".format(c,s))

This yields: 这样产生:

    age     sex
0   age = 1 sex = M
1   age = 2 sex = F

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

相关问题 如何使用python pandas向下一个单元格添加值 - how to add a value to the next cell with python pandas 如何将dict写入具有列表作为其值的csv并为其添加标题 - How to write a dict to a csv that have a list as its value and add a header for it 如何在 Python networkx 中的节点名称旁边添加节点属性? - How to add node attributes next to node name in Python networkx? 在 Python 中,是否有一个库可以根据其名称设置/更新一个 Excel 单元的值? - In Python, is there a library to set/update the value of one Excel cell based on its name? 使用 python 将我们的 excel 值复制到下一个单元格 - copy our excel value to the next cell with python 如何使用 python 在已找到的另一个单元格旁边的 excel 单元格中查找值? - How to use python to find value in excel cell next to another cell already found? 如何使用 Python 在 Excel 文件名中包含单元格的值? - How to include the cell's value in the Excel file name with Python? 匹配值并在python中获取其列标题 - Match Value and Get its Column Header in python 如果其值等于“x”,如何获取 Json 键名 - Python - How to get Json key name if its value is equal to "x" - Python Python Excel:如何添加一个单元格值和一个常量,然后打印回excel - Python Excel: How to add a cell value and a constant and then print back to the excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM