如何将多列的单元格值连接成一列?

How to concatenate cell values from multiple columns into a single column?

注意:我已经找到部分解决方案,请参阅下面的更新部分。

我目前正在尝试从 3 个单独的列中获取数据并将它们合并到另一个指定的列中。为清楚起见,请参考下图,其中代码 1、代码 2 和代码 3 在“合并代码”列中全部连接在一起。 Example of desired outcome。我写了一个for循环,可以更新指定列中每个单元格的值,请参考下面的代码。

for rows in row_id:
# Build new cell value
new_cell = smartsheet.models.Cell()
new_cell.column_id = column_id_final  # column id of previously created column
    # A for loop to fill in the rows with information from other rows
    for rows in row_id:
        new_cell.value = smart.Sheets.get_sheet(1174866712913796)
        new_cell.strict = False

在第二个循环的 new_cell.value 部分,我可以简单地输入一个字符串 'value',然后用 [= 填充指定列 (column_id_final) 的所有行28=]。从这里开始,我的思考过程是着手打印给定行中的所有显示值。如果我能做到这一点,我可以简单地连接三个变量,每个变量都包含给定列的所有显示值。参考下面的代码看看我的尝试。

# For loops to extract display values from each cell in a given column
cell_id = ''
row_info = ''
# Get all of the information stored within the rows of a sheet
for row in sheet.rows:
    row_info += str(smart.Sheets.get_row(1174866712913796, row.id))
print(row_info)
# pull the display value from each component of the row_info variable
for cell in row_info.cell:
    cell_id += string(cell.display_value) #is there a display value property?
print(cell_id)

我运行关注的问题是如何显示指定列中所有单元格的值。我有办法做到这一点吗?

更新

感谢 Emi 的评论,我调查了 Pandas 并找到了解决我问题的部分方法。我现在可以将我想要的列连接成一个列,将此列上传到 smartsheet,然后将行移动到另一个 sheet。但是,当我移动行时,它会将它们发布到目标 sheet 的底部。我知道这是默认位置,但我不知道移动行功能是否有位置属性。请参考下面的代码以更好地了解我的过程。

def add_merged_column():
    sheet = smart.Sheets.get_sheet(1174866712913796)  # store sheet information in sheet variable
    col_names = [col.title for col in
                 sheet.columns]  # Define column names for data fram as being equal to the column titles in the sheet information variable
    rows = []  # define rows
    for row in sheet.rows:  # for loop to iterate through the rows of 'sheet' and create a cell list for each
        cells = []  # define cells
        for cell in row.cells:  # for each cell within every row append the updated cell value
            cells.append(cell.value)
        rows.append(cells)
    df = pd.DataFrame(rows, columns=col_names)  # create data frame using panda, define columns
    df = df.set_index('Unique ID')  # get rid of built in index, make desired merged column index
    df['Unique ID'] = df['Column 1'].map(str) + ' ' + df['Column 2'].map(str) + ' ' + df['Column 3'].map(
        str)  # concatenate the desired columns into a single column
    merged_data = df['Unique ID']  # assign a variable name to result
    merged_data.to_csv(_dir + '\excel\proc_data.csv')  # send merged_data to a csv file and specify file path
    print(merged_data)
    result = smart.Sheets.import_csv_sheet(_dir + '\Excel\proc_data.csv', header_row_index=0)  #  import newly saved csv file into smartsheet

    sheet_csv = smart.Sheets.get_sheet(result.data.id)

    ################################################################################################################

    # iterate through the rows array and build comma-delimited list of row ids
    row_ids = ''  # define row_id
    for row in sheet_csv.rows:
        row_ids += str(
            row.id) + ', '  # the += adds to the existing variable each iteration, rather than defining an entirely new variable, this is what allows it to be a list, hence the addition of the ','
        # x += y is the same as x = x.__iadd__(y)

    # remove the final (excess) comma and space from the end of the row_ids string
    row_ids = row_ids[
              :len(row_ids) - 2]  # Specifies that row_ids is not equal to row_ids from index 0 -> length of the ids - 2

    row_ids = list(map(int, row_ids.split(',')))

    response = smart.Sheets.move_rows(result.data.id, smart.models.CopyOrMoveRowDirective(
        {'row_ids': row_ids, 'to': smart.models.CopyOrMoveRowDestination({'sheet_id': 1174866712913796})}))

    return merged_data

您可以为您的数据使用 pandas 数据框吗?我已经为您的数据 (df) 创建了一个示例数据框,但您可以从 csv 中提取它吗?我不确定您是如何存储数据的。

df = pd.DataFrame({'code 1':[423, 456], 'code 2':[657, 243], 'code 3':[568, 987]})

然后您可以将数字转换为字符串,然后像这样将它们连接在一起:

df = df.astype(str)
df['Merged Code'] = df['code 1'] + ' ' + df['code 2'] + ' ' + df['code 3']