使用数据框列名称和索引填充数据框单元格条目

时间:2019-06-22 22:01:22

标签: python-3.x pandas

我尝试使用以下方法填充数据名声:

  • 我生成了一个mxn大小的数据框
  • 数据框的列名称为A to N,并从传递给该方法的列表中读取。
  • 定义数据框的索引。
  • 用列名+ _ +索引填充数据框条目

import numpy as np
import pandas as pd
from tabulate import tabulate

def generate_data(N_rows, N_cols,names_df =[]):
    if N_rows == 4:
        d16 = ['RU19-24', 'RU13-18', 'RU7-12', 'RU1-6']
        df = pd.DataFrame(np.zeros((N_rows, N_cols)), index=d16 ,columns=names_df)
    else:
        print("The Elevation for each domain is defined by 4, you defined elevation: ", N_rows)
        df = None
        # df.loc[[],'Z'] = 3

    return tabulate(df, headers='keys', tablefmt='psql')


a = generate_data(4,2, ['A', 'B'])
print(a)

出局:

+---------+-----+-----+
|         |   A |   B |
|---------+-----+-----|
| RU19-24 |   0 |   0 |
| RU13-18 |   0 |   0 |
| RU7-12  |   0 |   0 |
| RU1-6   |   0 |   0 |
+---------+-----+-----+
Is it possible to take the index and concatenate with the column names to get the following output ? 
+---------+-------------+-------------+
|         |           A |   B         |
|---------+-------------+-------------|
| RU19-24 |   A_RU19-24 |   B_RU19-24 |
| RU13-18 |   A_RU13-18 |   B_RU13-18 |
| RU7-12  |   A_RU7-12  |   B_RU7-12  |
| RU1-6   |   A_RU1-6   |   B_RU1-6   |
+---------+-------------+-------------+

1 个答案:

答案 0 :(得分:2)

IIUC,您可以使用apply,它将数据帧的每一列作为pd.Series,并带有索引(数据帧索引)和序列名称(数据帧列标题):

df = pd.DataFrame(index=['RU19-24','RU13-18','RU7-12','RU1-6'], columns = ['A','B'])

df.apply(lambda x: x.name+'_'+x.index)

输出:

                 A          B
RU19-24  A_RU19-24  B_RU19-24
RU13-18  A_RU13-18  B_RU13-18
RU7-12    A_RU7-12   B_RU7-12
RU1-6      A_RU1-6    B_RU1-6

或使用np.add.outer

df = pd.DataFrame(index=['RU19-24','RU13-18','RU7-12','RU1-6'], columns = ['A','B'])
df_out = pd.DataFrame(np.add.outer(df.columns+'_',df.index).T, index=df.index, columns=df.columns)
df_out

输出:

                 A          B
RU19-24  A_RU19-24  B_RU19-24
RU13-18  A_RU13-18  B_RU13-18
RU7-12    A_RU7-12   B_RU7-12
RU1-6      A_RU1-6    B_RU1-6