我在最初作为对象读取的DataFrame中有一个系列,然后需要将其转换为yyyy-mm-dd形式的日期,其中dd是月末.
作为一个例子,我有DataFrame df,其中Date作为对象列:
... Date ...
... 200104 ...
... 200508 ...
Run Code Online (Sandbox Code Playgroud)
当这一切都说完了,我想要的是一个日期对象:
... Date ...
... 2001-04-30 ...
... 2005-08-31 ...
Run Code Online (Sandbox Code Playgroud)
这样df ['Date'].item()返回
datetime.date(2001, 04, 30)
Run Code Online (Sandbox Code Playgroud)
我已经使用以下代码几乎到了那里,但我所有的日期都是在月初,而不是结束.请指教.
df['Date'] = pd.to_datetime(df['Date'], format="%Y%m").dt.date
Run Code Online (Sandbox Code Playgroud)
注意:我已经导入了Pandas ad pd,日期时间为dt
问题简化:
我需要根据列中DataFrame的文本是否具有“-”字符来提取和修改 a 的特定行。破折号和后面的所有内容都需要删除,剩余的文本需要是“-”之前的任何内容。
have:
textcol
0 no dash here
1 one - here
want:
textcol
0 one
Run Code Online (Sandbox Code Playgroud)
这是用于重新创建我的场景的代码。
df = pd.DataFrame(data=['no dash here', 'one - here'], index=[0, 1], columns=['textcol'])
df2 = df[df['textcol'].str.contains('-') == True]
df2.loc[:, ['textcol']] = df2['textcol'].str.split('-').str[0]
Run Code Online (Sandbox Code Playgroud)
结果DataFramedf2 产生了我想要的结果,只有一个例外。每次我打电话给 df2(或之后的任何衍生产品)时,我都会收到以下信息SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
Run Code Online (Sandbox Code Playgroud)
我试图以不同的方式完成我想要的事情,并得到了一个类似的错误,指示我尝试使用 .loc()功能,但我仍然收到这个类似的错误。
有没有更好的、无错误威胁的方式来完成这个结果?恐怕这里发生了一些我不明白的事情,最终 df2 不会产生我想要的结果。我也想知道类似的东西.query()是否可行。
我有一个索引为 3 个级别的数据框。我需要按每个级别但以不同的方式对索引进行排序。什么可以做到这一点?
有一个数据框 ( df) 为:
other columns
color shape count
red circle 1 x
triangle 3 x
2 x
blue circle 4 x
triangle 2 x
Run Code Online (Sandbox Code Playgroud)
我希望有一个新的df地方color进行排序ascending,shape是descending和count是ascending:
other columns
color shape count
blue triangle 2 x
circle 4 x
red triangle 2 x
3 x
circle 1 x
Run Code Online (Sandbox Code Playgroud) 我有一个 python 脚本,我想在我的 unix 服务器的后台执行。问题是我需要 python 脚本等待上一步完成,然后才能进入下一个任务,但我希望我的工作在退出后继续运行。
我想我可以设置如下,但想确认:
脚本的摘录如下所示,其中命令 2 依赖于命令 1 的输出,因为它在同一目录中输出一个已编辑的可执行文件。我想指出的是,命令 1 和 2 没有包含 nohup/&。
subprocess.call('unix command 1 with options', shell=True)
subprocess.call('unix command 2 with options', shell=True)
Run Code Online (Sandbox Code Playgroud)
如果当我像这样启动我的 python 脚本时:
% nohup python python_script.py &
Run Code Online (Sandbox Code Playgroud)
我的脚本是否会在后台运行,因为我明确没有将 nohup/& 放在我的脚本 unix 命令中,而是在后台运行 python 脚本?
数据框是这样设置的。我想要一个具有多级帐户索引的数据透视表,伪。我希望列是单独的周,值是销售额。该数据是伪 x 周 x 帐户,因此我不需要聚合任何数据。我怎样才能完成?
pseudo week account sales
0 31527 2017-12-30 4430012511 2.79
1 145584 2017-12-16 4430012511 8.37
2 31608 2017-12-23 4430012511 19.53
3 6362 2017-12-16 4430012511 5.58
Run Code Online (Sandbox Code Playgroud) python ×5
pandas ×4
dataframe ×1
date ×1
datetime ×1
multi-index ×1
nohup ×1
pivot ×1
pivot-table ×1
reshape ×1
sorting ×1
subprocess ×1
unix ×1