[英]Create a dataframe from some rows of another dataframe
Here is the DataFrame I am working with, for reference.这是我正在使用的 DataFrame,供参考。
data2 = {'col10':[1.0, 2.0, 3.0, 4.0], 'col11':[100, 200, 300, 400]}
df = pd.DataFrame(data2, index = ['a', 'b', 'c', 'd'])
I'm trying to create a new DataFrame, df2, from the last two rows of this column.我正在尝试从该列的最后两行创建一个新的 DataFrame,df2。 I'm not sure how to do this and would appreciate some tips.
我不确定如何做到这一点,并希望得到一些提示。
Try this:尝试这个:
newdf=df.loc[['c','d']]
>>> print(newdf)
col10 col11
c 3.0 300
d 4.0 400
If you want a more generic solution, for example if you don;t know the index of your last tow rows, you can use the following:如果您想要一个更通用的解决方案,例如,如果您不知道最后两行的索引,您可以使用以下内容:
newdf=df.iloc[-2:]
>>> print(newdf)
col10 col11
c 3.0 300
d 4.0 400
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.