我面临一个棘手的问题,我想从 Pandas 数据框中将所有其他列子集到另一个数据框中,并将其从原始数据框中删除。这是一个示例:
firstname = ['John', 'Eric', 'Sarah', 'Kate']
lastname = ['Adams', 'Firth', 'Smith', 'Johnson']
df = pd.DataFrame({"First": first, "Last":last})
我理想的输出如下:原始数据帧包含名字约翰和莎拉的记录,第二个数据帧包含名字埃里克和凯特的记录。
任何帮助将不胜感激!
最佳答案
使用,iloc
df1, df2 = df.iloc[::2,:], df.iloc[1::2,:]
df1
输出:
First Last
0 John Adams
2 Sarah Smith
而且, df2 输出:
First Last
1 Eric Firth
3 Kate Johnson
关于python - 对 Pandas 中的每隔一行进行子集化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58033915/