在DataFrame中插入新行,并将另一行的内容粘贴到新行。

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

Inserting a new row on a DataFrame and and pasting the contents of another row to that new row

问题

我想要将一行插入到我的数据框中的特定索引位置。一旦我这样做了,我想复制并粘贴另一行的内容到新创建的行中,然后删除旧行。我已经成功使用numpy创建了一个空行,但问题是它将列名更改为数字。请协助。

import pandas as pd

df = pd.DataFrame({"var1": ["AZZBBAA", "CCDDDED", "DZZZZFD", "CDEEEEFG"],
                  "var2": [1, 2, 4, 5]})

# 插入新行:
import numpy as np
df = pd.DataFrame(np.insert(df.values, 2, '', axis=0))

# 输出如下:
      0   1
0  AZZBBAA   1
1  CCDDDED   2
2
3  DZZZZFD   4
4  CDEEEEFG  5

上面的输出确实插入了新行,但现在更改了列名。

我的期望输出是,我可以复制索引为3的行"CDEEEEFG 5"到新的第2行,然后删除我复制的那行。

# 期望输出
     var1   var2
0  AZZBBAA     1
1  CCDDDED     2
2  CDEEEEFG   5
3  DZZZZFD     4
英文:

I want to insert a new row into my dataframe on to a specific index. And once I have done so I want to copy and paste the contents of another row into that new row created and delete the old row. I managed to create an empy row using numpy but the problem is that it changes the column names to number. Please assist.

import pandas as pd

df = pd.DataFrame({"var1": ["AZZBBAA", "CCDDDED", "DZZZZFD", "CDEEEEFG"],
                  "var2": [1,2,4,5]})
Output is :

	var1	   var2
0	AZZBBAA	    1
1	CCDDDED	    2
2	DZZZZFD	    4
3	CDEEEEFG	5

#inserting a new row:

import  numpy as np
df = pd.DataFrame(np.insert(df.values, 2,'', axis=0))

#output is below.
	0	      1
0	AZZBBAA	  1
1	CCDDDED	  2
2
3	DZZZZFD	  4
4	CDEEEEFG  5

The ouput above does insert a new row, but it now changes the column names.

My desired output is where I can copy row 4index3 ```CDEEEEEFG 5```` into the now new row on index 2 and delete the one that I copied.

#desired output
	var1	  var2
0	AZZBBAA	    1
1	CCDDDED	    2
2   CDEEEEFG	5
3	DZZZZFD	    4

答案1

得分: 2

我认为插入不是必要的 - 只需重命名索引以指定新的顺序,然后使用DataFrame.sort_index来获得预期的输出:

n = 2
m = 3

df = df.rename({m: n-0.5}).sort_index(ignore_index=True)
print (df)
       var1  var2
0   AZZBBAA     1
1   CCDDDED     2
2  CDEEEEFG     5
3   DZZZZFD     4
英文:

I think inserting is not necessary - only rename indices for specify new order, then add DataFrame.sort_index for expected ouput:

n = 2
m = 3

df = df.rename({m: n-0.5}).sort_index(ignore_index=True)
print (df)
       var1  var2
0   AZZBBAA     1
1   CCDDDED     2
2  CDEEEEFG     5
3   DZZZZFD     4

答案2

得分: 0

如果您知道数据框索引的顺序,那么可以直接使用 iloc,因为您只是将一行的内容复制到另一行。只需重新排列您的数据框。

df = pd.DataFrame({"var1": ["AZZBBAA", "CCDDDED", "DZZZZFD", "CDEEEEFG"],
                  "var2": [1, 2, 4, 5]})
df2 = df.iloc[[0, 1, 3, 2]].reset_index()

输出:

   index      var1  var2
0      0   AZZBBAA     1
1      1   CCDDDED     2
2      3  CDEEEEFG     5
3      2   DZZZZFD     4
英文:

If you know the order of the dataframe indices you'd like then you can just use iloc since you're just copying contents from one row to another anyway. Just reorder your dataframe.

df = pd.DataFrame({"var1": ["AZZBBAA", "CCDDDED", "DZZZZFD", "CDEEEEFG"],
                  "var2": [1,2,4,5]})
df2 = df.iloc[[0, 1, 3, 2]].reset_index()

Out[43]: 
   index      var1  var2
0      0   AZZBBAA     1
1      1   CCDDDED     2
2      3  CDEEEEFG     5
3      2   DZZZZFD     4

huangapple
  • 本文由 发表于 2023年2月14日 19:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446889.html
匿名

发表评论

匿名网友

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

确定