我有一个类似于这个的DataFrame对象:
onset length
1 2.215 1.3
2 23.107 1.3
3 41.815 1.3
4 61.606 1.3
...
Run Code Online (Sandbox Code Playgroud)
我想要做的是在某个索引值指定的位置插入一行,并相应地更新以下索引.例如:
onset length
1 2.215 1.3
2 23.107 1.3
3 30.000 1.3 # new row
4 41.815 1.3
5 61.606 1.3
...
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
我需要为每个组插入缺失的类别,这是一个例子:
import pandas as pd
import numpy as np
df = pd.DataFrame({ "group":[1,1,1 ,2,2],
"cat": ['a', 'b', 'c', 'a', 'c'] ,
"value": range(5),
"value2": np.array(range(5))* 2})
df
# test dataframe
cat group value value2
a 1 0 0
b 1 1 2
c 1 2 4
a 2 3 6
c 2 4 8
Run Code Online (Sandbox Code Playgroud)
说我有一些categories = ['a', 'b', 'c', 'd']。如果cat列不包含列表中的类别,我想为每个具有 value 的组插入一行0。如何在每组插入一行if category,以便获取每组的所有类别
cat group value value2
a 1 0 0
b 1 1 …Run Code Online (Sandbox Code Playgroud)