【问题标题】:How to move specific data from one column to a new column on Pandas?如何将特定数据从一列移动到 Pandas 的新列?
【发布时间】:2021-03-24 19:59:20
【问题描述】:

我有一组包含 2 列的数据:Column1 = 十六进制代码和 Column2= 当前 (A)。

Column1 中的数据是 Hex Code,27 个不同的重复代码,每个 Hex Code 在 Column2 上都有 Current (A) 值。

我想从 Column1 和 Column2 中挑选一组 27 个数据点并将它们放入 Coulmn3 和 Column4 中。

有人可以帮我实现吗?

This is how the initial data looks

This is how i would like the data to be arranged

【问题讨论】:

标签: python excel pandas dataframe


【解决方案1】:

我将向您展示我的代码。但我想告诉你,你不能有重复的列名。我们假设 data 是您的原始数据集的名称:

import pandas as pd

col_name1=data.columns.values[0]
col_name2=data.columns.values[1]

two_columns = data[[col_name1,col_name2]][0:27].values

two_columns = pd.DataFrame(two_columns,columns=[col_name1+'_1',col_name2+'_2'])

df = data.iloc[0:27,:]

df = df.join(two_columns)

print(df)

【讨论】:

    猜你喜欢
    • 2022-11-29
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2022-10-14
    • 2021-10-16
    • 2023-02-23
    • 2014-03-13
    相关资源
    最近更新 更多