Python 2.7 如何使用数组有选择地从数据帧复制行?
我有一个带索引号的数组Python 2.7 如何使用数组有选择地从数据帧复制行?,python-2.7,pandas,dataframe,machine-learning,Python 2.7,Pandas,Dataframe,Machine Learning,我有一个带索引号的数组 array=[2,3,4,5,234,653.......2500,2501] 我需要将带有这些索引号的DF A中的所有行从数组复制到一个新的DF易用loc: 将数组传递给loc将选择索引位于数组中的行。难道不能循环遍历索引编号数组,并将该值用作DF数组的索引,然后将第二个数组的结果值添加到第三个初始为空的DF数组中吗? # Random array idx array([1, 4, 6]) # random dataframe df a 0 1 1
array=[2,3,4,5,234,653.......2500,2501]
我需要将带有这些索引号的DF A中的所有行从数组复制到一个新的DF易用loc:
将数组传递给loc将选择索引位于数组中的行。难道不能循环遍历索引编号数组,并将该值用作DF数组的索引,然后将第二个数组的结果值添加到第三个初始为空的DF数组中吗?
# Random array
idx
array([1, 4, 6])
# random dataframe
df
a
0 1
1 3
2 5
3 7
4 7
5 34
6 3
7 24
new_df = df.loc[idx]
new_df
a
1 3
4 7
6 3