简体   繁体   English

如何将数据框的列标题复制到每个行值?

[英]How to copy the column headers of a data frame to each row value?

I'm trying to transform a pandas data frame, applying a function to all values, to concatenate the column header and the actual value. 我正在尝试转换pandas数据帧,将函数应用于所有值,以连接列标题和实际值。

My original dataframe is something like this: 我的原始数据框是这样的:

|---------------------|------------------|
|         Col1        |        Col2      |
|---------------------|------------------|
|          12         |         34       |
|---------------------|------------------|
|          12         |         34       |

The output should be: 输出应该是:

|---------------------|------------------|
|       Col1          |      Col2        |
|---------------------|------------------|
|       Col1_12       |      Col2_34     |
|---------------------|------------------|
|       Col1_12       |      Col2_34     |

What I tried is this: 我试过的是这个:

mypandasdf.applymap(lambda x: 'columnname'+'_'+str(x))

But I'm struggling with the column name value. 但我正在努力争取列名值。 How can I put the real column name instead of a string? 如何将实际列名称而不是字符串? Or is there any other/better way to do it? 或者还有其他/更好的方法吗?

df = pd.DataFrame({'colA': [12,34], 'colB': [56,78]})

df = df.columns.values + '_' + df.astype(str)

print(df)

Output: 输出:

      colA     colB
0  colA_12  colB_56
1  colA_34  colB_78

Use DataFrame.columns to access each column and concatenate as string to each value: 使用DataFrame.columns访问每个列并将字符串连接到每个值:

for col in df.columns:
    df[col] = col + '_' + df[col].astype(str)

print(df)
      Col1     Col2
0  Col1_12  Col2_34
1  Col1_12  Col2_34

Convert each row in your dataframe as string and concat the column name: 将数据框中的每一行转换为字符串并连接列名:

df = pd.DataFrame({
                   "col1": [12 , 34],
                   'col2': [7,9]},)
for c in df:
    df[c] = c + '_' + df[c].astype(str)
df

Result: 结果:

    col1    col2
0   col1_12 col2_7
1   col1_34 col2_9

IIUC IIUC

df.apply(lambda x : x.name+'_'+x.astype(str))
Out[1323]: 
      Col1     Col2
0  Col1_12  Col2_34
1  Col1_12  Col2_34

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

相关问题 如何为循环中的每一行数据添加列标题? - How do I add column headers for each row of data in a loop? 如何用列值替换pandas数据框中的每个值? - How to replace each value in pandas data frame with column value? 如何通过匹配数据框中的行值来更改列值? - How to change column value by matching row value in data frame? 转置列数据并将其复制到每一行 - Transpose and copy column data to each row 如果列值满足语句,如何提取整个数据框行? - How to extract an entire data frame row if column value fulfills a statement? 你如何连接 pandas 数据帧中行的列值? - how do you concatenated column value for row in pandas data frame? 如何将值列表中的数据框转换为大数据框,其中每个级别在python中为单列值? - how to convert a data frame with a list in the value to a big data frame with the each level as a single column value in python? 如何根据列名将一个数据框中的列值复制到另一个数据框中? - How do I copy the value of columns in one data frame to another data frame based on column names? 计算数据框中列的每一行中数组的每个唯一数组 - Counting each unique array of an array in each row of a column in a data frame 根据列值突出显示数据框中的行? - Highlight row in a data frame based on a column value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM