本文介绍了使用Python获取列的两个值之间的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设存在一个数据帧,如下所示:
Suppose there is a data frame as follows:
df = {
'Period': [1996,'Jan','Feb','March',1997,'Jan','Feb','March',1998,'Jan','Feb','March']
'Some-Values': [,'a','b','c',,'d','e','f',,'g',h','i']
}
以及需要提取值1996
和1997
之间的行,以便得到的数据帧如下:
and the rows between the values 1996
and 1997
needs to be extracted such that the resulting data frame is as follows:
df_res = {
'Period': ['Jan','Feb','March']
'Some-Values': ['a','b','c']
}
我目前正在为此尝试熊猫,但找不到解决方案.
I am currently trying Pandas for this but am unable to find a solution.
推荐答案
尝试将您的数据框更改为正确"方式,然后我们就可以使用年份信息来获取信息
Try to change your dataframe into "correct" way , then we can getting the information by using year information
df['Year']=df.loc[df['Some-Values']=='','Period']
df.Year=df.Year.ffill()
df=df.loc[df.Period!=df.Year,:]
df.loc[df.Year==1996,:]
Out[651]:
Period Some-Values Year
1 Jan a 1996
2 Feb b 1996
3 March c 1996
这篇关于使用Python获取列的两个值之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!