如何从pandas数据框中提取列,将其附加到行,同时保持其他列不变。
这是我的示例数据集。
import pandas as pd
import numpy as np
df = pd.DataFrame({'ID': np.arange(0,5),
'sample_1' : [5,6,7,8,9],
'sample_2' : [10,11,12,13,14],
'group_id' : ["A","B","C","D","E"]})
我正在寻找的输出是:
df2 = pd.DataFrame({'ID': [0, 1, 2, 3, 4, 0, 1, 2, 3, 4],
'sample_1' : [5,6,7,8,9,10,11,12,13,14],
'group_id' : ["A","B","C","D","E","A","B","C","D","E"]})
我尝试使用pd.concat
对数据帧和concat进行切片,但是它给出了NaN值。
我的原始数据集很大。
答案 0 :(得分:2)
您可以使用widget.datasource.createItem({
success: function(record){
console.log(record._key);
},
failure: function(error){
var err = error.toString();
if(err.indexOf("Duplicate entry") > -1){
alert("There is already a schedule on that date");
} else {
alert(error.toString());
}
}
});
进行此操作:将索引设置为您不想修改的列,调用堆栈,按“样本”列排序,然后重置索引:
stack
答案 1 :(得分:2)
res = pd.wide_to_long(df, stubnames='sample_', i='ID', j='group_id')
res.index = res.index.droplevel(1)
res = res.rename(columns={'sample_': 'sample_1'}).reset_index()
print(res)
ID group_id sample_1
0 0 A 5
1 1 B 6
2 2 C 7
3 3 D 8
4 4 E 9
5 0 A 10
6 1 B 11
7 2 C 12
8 3 D 13
9 4 E 14
答案 2 :(得分:1)
您要查找的函数称为melt
例如:
df2 = pd.melt(df, id_vars=['ID', 'group_id'], value_vars=['sample_1', 'sample_2'], value_name='sample_1')
df2 = df2.drop('variable', axis=1)