Pandas 反向循环数据帧行
我试图以相反的顺序循环数据帧行 基于行位置而不是索引名 我认为这个代码应该可以工作,但它不是Pandas 反向循环数据帧行,pandas,loops,Pandas,Loops,我试图以相反的顺序循环数据帧行 基于行位置而不是索引名 我认为这个代码应该可以工作,但它不是 for i, row in enumerate(df[::-1].iterrows()): print (i) 当我运行它时,它会产生 0 1 2 3 4 5 而不是 5 4 3 2 1 0 关于如何使用问题的iterrows()的注释提供了反向循环通过DataFrame行的答案。为了简单起见,它还引入了使用列表理解的思想 对于越来越大的数据集,将遇到性能和内存问题。有一种更有效的方法
for i, row in enumerate(df[::-1].iterrows()):
print (i)
当我运行它时,它会产生
0
1
2
3
4
5
而不是
5
4
3
2
1
0
关于如何使用问题的
iterrows()
的注释提供了反向循环通过DataFrame
行的答案。为了简单起见,它还引入了使用列表理解的思想
对于越来越大的数据集,将遇到性能和内存问题。有一种更有效的方法可以反向访问数据帧中的数据
以下内容有助于为新用户提供指导。要点是将dataframe索引标签放入一列中,该列将创建一个有序的新索引,保留行位置,因此可以反转
import pandas as pd
import numpy as np
import timeit
print(pd.__version__)
# random dataframe, provides ordered rangeindex
df = pd.DataFrame(np.random.randint(0,1000,size=(1000, 4)), columns=list('ABCD'))
# toss the ordered rangeindex and make the random 'A' the index
df.set_index(['A'], inplace=True)
# df is now a dataframe with an unordered index
def iterate(df):
for i,r in df[::-1].iterrows():
# process
pass
def sort_and_apply(df):
# apply order to the index by resetting it to a column
# this indicates original row position by create a rangeindex.
# (this also copies the dataframe, critically slowing down this function
# which is still much faster than iterate()).
new_df = df.reset_index()
# sort on the newly applied rangeindex and process
new_df.sort_index(ascending=False).apply(lambda x:x)
if __name__ == '__main__':
print("iterate ", timeit.timeit("iterate(df)", setup="from __main__ import iterate, df", number=50))
print("sort_and_apply ",timeit.timeit("sort_and_apply(df)", setup="from __main__ import sort_and_apply, df", number=50))
产生
0.24.2
iterate 2.893160949
sort_and_apply 0.12744747599999995
关于如何使用问题的iterrows()
的注释提供了反向循环通过DataFrame
行的答案。为了简单起见,它还引入了使用列表理解的思想
对于越来越大的数据集,将遇到性能和内存问题。有一种更有效的方法可以反向访问数据帧中的数据
以下内容有助于为新用户提供指导。要点是将dataframe索引标签放入一列中,该列将创建一个有序的新索引,保留行位置,因此可以反转
import pandas as pd
import numpy as np
import timeit
print(pd.__version__)
# random dataframe, provides ordered rangeindex
df = pd.DataFrame(np.random.randint(0,1000,size=(1000, 4)), columns=list('ABCD'))
# toss the ordered rangeindex and make the random 'A' the index
df.set_index(['A'], inplace=True)
# df is now a dataframe with an unordered index
def iterate(df):
for i,r in df[::-1].iterrows():
# process
pass
def sort_and_apply(df):
# apply order to the index by resetting it to a column
# this indicates original row position by create a rangeindex.
# (this also copies the dataframe, critically slowing down this function
# which is still much faster than iterate()).
new_df = df.reset_index()
# sort on the newly applied rangeindex and process
new_df.sort_index(ascending=False).apply(lambda x:x)
if __name__ == '__main__':
print("iterate ", timeit.timeit("iterate(df)", setup="from __main__ import iterate, df", number=50))
print("sort_and_apply ",timeit.timeit("sort_and_apply(df)", setup="from __main__ import sort_and_apply, df", number=50))
产生
0.24.2
iterate 2.893160949
sort_and_apply 0.12744747599999995
如果你接受重新编制索引,你也可以这样做
for i, row in enumerate(df.reindex().sort_index(ascending=False):
print (i)
如果你接受重新编制索引,你也可以这样做
for i, row in enumerate(df.reindex().sort_index(ascending=False):
print (i)
尝试使用[i for i,r in df[::-1].iterrows()]
给出:[3,2,1,0]
基本上摆脱枚举()
尝试使用[i for i,r in df[:-1].iterrows()
给出:[3,2,1,0]
基本上摆脱枚举()