社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  sacuL  »  全部回复
回复总数  4

您得到一个object列的事实告诉我,有些值不能解释为适当的时间增量。如果是这样的话,我会用 pd.to_timedelta 带着争论 errors='coerce' 然后调用 dt.days :

df['age'] = pd.to_timedelta(df['age'],errors='coerce').dt.days
>>> df
   id   age
0   1  6252
1   2  1800
2   3  5873

这不一定是最优雅的,但它是有效的:找到任何一点 motion 1 ,或在何处 运动 向任一方向移动1是 . 有两种方法 numpy 功能(注意 麻木的 函数不需要显式导入 麻木的 ,因为它们也内置在 pandas 可以通过 pd.np ,但请参见@abhi的评论 熊猫 等效值):

df['motion2'] = pd.np.where(df.motion.values|pd.np.roll(df.motion.values,1)|pd.np.roll(df.motion.values,-1),1,0)

# The following is Essentially the equivalent, but maybe a bit clearer / more efficient
df['motion2'] = pd.np.stack((df.motion.values,pd.np.roll(df.motion.values,1),pd.np.roll(df.motion.values,-1))).any(0).astype(int)

>>> df
    device  time  motion  motion2
0        1     1       0        0
1        1     2       0        1
2        1     3       1        1
3        1     4       1        1
4        1     5       1        1
5        1     6       0        1
6        1     7       0        0
7        1     8       0        1
8        1     9       1        1
9        1    10       1        1
10       1    11       0        1
11       1    12       0        0
12       2     5       0        0
13       2     6       0        0
14       2     7       0        1
15       2     8       1        1
16       2     9       1        1
17       2    10       1        1
18       2    11       0        1
19       2    12       1        1
20       2    13       0        1
21       2    14       0        0
6 年前
回复了 sacuL 创建的主题 » 如何使美元符号出现在python中的和旁边?[副本]

尝试

print("The total of the three items is:${}".format(total))

如果在python 3.6或更高版本中,也可以使用 f-Strings :

print(f"The total of the three items is:${total}")

“老式的方法”是使用 %s 格式化程序:

print("To total of the three items is:$%s" % total)
6 年前
回复了 sacuL 创建的主题 » 使用python pandas根据多个条件选择行

在你的 loc 语句,需要修复括号并在条件之间添加括号:

df.loc[(df['FL_DATE'] == input1) & (df['ORIGIN'] == 'ATL') & (df['DEST'] == input2)]

然后它开始工作:

>>> df.loc[(df['FL_DATE'] == date) & (df['ORIGIN'] == 'ATL') & (df['DEST'] == destination)]

    FL_DATE ORIGIN DEST  DEP_TIME
0  5/1/2017    ATL  IAD      1442