Python 3.x 如何创建给定值的数据帧
通过以下代码,我得到:Python 3.x 如何创建给定值的数据帧,python-3.x,pandas,Python 3.x,Pandas,通过以下代码,我得到: import pandas as pd date=['1/3/15','2/5/15','3/6/15','4/8/16'] dist=[5,4,11,12] dd=list(zip(date,dist)) df=pd.DataFrame(dd,columns=['Date','Dist']) print(df) 输出: 我只想获得dist>10和相应的日期,如下所示: Date Dist 2 3/6/15 11 3 4/8/16 12 我尝试了以下方法:
import pandas as pd
date=['1/3/15','2/5/15','3/6/15','4/8/16']
dist=[5,4,11,12]
dd=list(zip(date,dist))
df=pd.DataFrame(dd,columns=['Date','Dist'])
print(df)
输出:
我只想获得dist>10和相应的日期,如下所示:
Date Dist
2 3/6/15 11
3 4/8/16 12
我尝试了以下方法:
dd10=pd.DataFrame(df['Dist']>10)
print(dd10)
这只会导致:
0 False
1 False
2 True
3 True
Name: Dist, dtype: bool
如何将所需结果设置为int,并使用相应的日期而不是bool?调用它并需要df[mask]
:
df1 = df[df['Dist']>10]
另一种过滤方法是:
df1 = df[df['Dist']>10]
df1 = df.query("Dist > 10")
print (df1)
Date Dist
2 3/6/15 11
3 4/8/16 12