我有一些数据框,我想分别从它们中提取前3列和后3列。并且它们的列名和列数不同。谁能给我一个提示,我该怎么做?先感谢您 ; )
df <- matrix(sample(1:10, 100, replace = TRUE), 10)
df <- as.data.frame(df)
#I know we can do something like this
df[,c(1:3,5:6)]
#but I dont know how to select last 3 columns
答案 0 :(得分:4)
使用#csv_columns = ['Name', 'Price', 'Link','Product URL', 'Merchant 1 Link',
#'Merchant 2 Link', 'Amazon Image','Merchant 1 Image', 'Merchant 2 Image', 'Notes']
ordered_list = csv_columns
df = pd.DataFrame(items)
df.to_excel("output.xlsx",sheet_name='Sheet1')
wb=openpyxl.load_workbook('output.xlsx')
#convert Image Links to actual images
wb.save('number1.xlsx')
wb.close()
INSERT INTO t(val)
VALUES(1),(2),(2),(3),(4),(4),(5);
SELECT
val,
RANK() OVER (
ORDER BY val
) my_rank
FROM
t;
或ncol()
df[,c(1:3, (ncol(df) - 2):ncol(df))]
# V1 V2 V3 V8 V9 V10
#1 2 3 8 9 10 1
#2 7 7 7 9 3 6
#3 8 2 10 7 7 4
#4 1 1 3 5 8 5
#5 1 5 8 9 1 1
#6 7 10 5 8 7 1
#7 5 3 3 3 10 4
#8 8 9 1 3 5 6
#9 6 1 6 8 7 7
#10 7 2 9 10 10 9
答案 1 :(得分:4)
我们可以在列名上使用head
和tail
df[, c(head(names(df), 3), tail(names(df), 3))]