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