将特定的DataFrame行值提取并移动到一个新列中。

huangapple go评论74阅读模式
英文:

Taking specific DataFrame row values and moving them to another new column

问题

我有一个如下的DataFrame:

Column1  Column2
'a'      'b'
'amount' '$d'
'e'      'f'
'amount' '$g'
...

我创建了一个新的列'Column3',我想要获取$d,$g等值(任何直接在'amount'值右边的值),并将它们移到Column3,同时去掉'amount'的值:

Column1  Column2  Column3
a        b        $d
e        f        $g
...

将这些值移到Column3并去掉'amount'值的最佳方法是什么?

英文:

I have a DataFrame as follows:

Column1  Column2
'a'      'b'
'amount' '$d'
'e'      'f'
'amount' '$g' 
...

I created a new column 'Column3', where I would like to take the values of $d, $g, etc. (any value that is directly right to a value 'amount') and move them to Column 3, while also getting rid of value 'amount':

Column1  Column2  Column3
a        b        $d
e        f        $g
...

What would be the best way to move these values to Column3 and remove 'amount' values?

EDIT: Made the values more specific to the question.

答案1

得分: 1

这里是一种方法:

  • 创建一个布尔系列,其中True表示"Column1"等于"amount"(Series.eq)。
  • 对"Column2"应用Series.where以获取"amount"旁边的值,并使用df.shift将结果向上移动一行。
  • 然后,我们使用df.assign将该系列添加到df中,并使用布尔系列的否定(~) 从df中选择。
  • 连锁使用df.reset_index以获得"干净"的索引。
m = df['Column1'].eq('amount')
res = df.assign(Column3=df['Column2'].where(m).shift(-1))[~m].reset_index(drop=True)

res

  Column1 Column2 Column3
0       a       b      $d
1       e       f      $g
英文:

Here's one approach:

  • Create a boolean series with True where "Column1" equals "amount"(Series.eq).
  • Apply Series.where to "Column2" to get values next to "amount", and use df.shift to shift the result one row up.
  • Next, we add the series to the df with df.assign, and select from the df using the inverse (~) of the boolean series.
  • Chain df.reset_index for a "clean" index.
m = df['Column1'].eq('amount')
res = df.assign(Column3=df['Column2'].where(m).shift(-1))[~m].reset_index(drop=True)

res

  Column1 Column2 Column3
0       a       b      $d
1       e       f      $g

答案2

得分: 1

With join/shift :

out = df[df["Column1"].ne("amount")].join(df["Column2"].rename("Column3").shift(-1))


Output :

print(out)

  Column1 Column2 Column3
0       a       b      $d
2       e       f      $g
英文:

With join/shift :

out = df[df["Column1"].ne("amount")].join(df["Column2"].rename("Column3").shift(-1))


Output :

print(out)

  Column1 Column2 Column3
0       a       b      $d
2       e       f      $g

答案3

得分: 1

以下是翻译好的部分:

这是一种执行方法:

  • 选择要移至新列的Column2中的值
  • 创建一个新的数据框,去掉那些["Column1"] != "c"的行,并将预先选择的值分配到新列中
  • (可选)如果需要,重置索引

示例:

# 预先选择要从`Column2`移动的值
data_to_move = df[df['Column1'] == 'c']['Column2'].values

# 删除`Column1`等于`c`的行,将预先选择的数据分配到新列中
reshaped_data = df[df['Column1'] != 'c'].copy().reset_index(drop=True)
reshaped_data['Column3'] = data_to_move
reshaped_data
英文:

Here is one way of doing it:

  • Select values from Column2 that you want to move to a new column
  • Create a new df, omitting rows where ["Column1"] != "c", and assign the pre-selected values to a new column
  • (optional) reset index if you want to

Example:

# Pre-select values you want to move from `Column2`
data_to_move = df[df['Column1'] == 'c']['Column2'].values

# Drop rows where 'Column1' equals 'c', assigned pre-selected data to a new column
reshaped_data = df[df['Column1'] != 'c'].copy().reset_index(drop=True)
reshaped_data['Column3'] = data_to_move
reshaped_data

答案4

得分: 1

你可以使用一个布尔遮罩:

m = df['Column1'] == 'amount'
df = df[~m].assign(Amount=df.loc[m, 'Column2'].values).reset_index(drop=True)

输出:

>>> df
  Column1 Column2 Amount
0       a       b     $d
1       e       f     $g
英文:

You can use a boolean mask:

m = df['Column1'] == 'amount'
df = df[~m].assign(Amount=df.loc[m, 'Column2'].values).reset_index(drop=True)

Output:

>>> df
  Column1 Column2 Amount
0       a       b     $d
1       e       f     $g

huangapple
  • 本文由 发表于 2023年4月11日 04:29:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定