- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 Excel 电子表格,其中一列包含一些日期时间数据。我使用 Pandas 将数据导出到数据框中。然而,该列有一些日期 block 交换了月份和日期,而同一列中的其他日期 block 是正确的。这是一个例子-
图 1:错误地交换了日期和月份
上图显示了交换的日期和月份。日期显示为 2016-01-10
,但应该改为 2016-10-01
。将其与同一列中的另一组日期时间值进行比较 -
图 2:正确表示日和月
在图 2 的上述示例中,月份正确表示为 12
,日期为 31
。
我使用了这个问题的解决方案 - How to swap months and days in a datetime object?
我也尝试过使用这个解决方案 - Python Pandas - Day and Month mix up
我也尝试编写自己的函数来映射到条目,但这也无济于事 -
def dm_swap(day, month):
if(month != 10 or month != 11 or month != 12):
temp = day
day = month
month = temp
t2016Q4.start.map(dmswap, t2016Q4.dt.day, t2016Q4.dt.month)
但是,这两种解决方案都会更改列中的所有日期时间值。因此,当不正确的值得到更正时,正确的值就会变得不正确。
为了方便起见,我还链接了 excel 文件。这是一个开放的数据集。
请选择最后一个数据集 Bikeshare Ridership (2016 Q4)。 “开始”和“结束”列存在上述问题。
是否有更有效的方法来清理日期时间数据?
最佳答案
好的。
再次编辑。我运行了下面的代码,它花了很长时间!我最后中止了,但这在合理的时间内肯定也有效 - 祝你好运!:
import pandas as pd
f = "string\to\file\here.xlsx"
df = pd.read_excel(f)
def alter_date(timestamp):
try:
date_time = timestamp.to_datetime().strftime("%Y-%d-%m %H:%M:%S")
time_stamp = pd.Timestamp(date_time)
return time_stamp
except:
return timestamp
new_starts = df["trip_start_time"].apply(alter_date)
df["trip_start_time"] = new_starts
new_ends = df["trip_stop_time"].apply(alter_date)
df["trip_stop_time"] = new_ends
编辑:我进行了一些挖掘,根据我之前所做的,这看起来是可能的,这里是新代码:
import pandas as pd
f = "string\to\file\here.xlsx"
df = pd.read_excel(f)
for idx in df.index:
trip_start = df.loc[df.index[idx], "trip_start_time"]
trip_end = df.loc[df.index[idx], "trip_stop_time"]
start_dt = trip_start.to_datetime()
end_dt = trip_end.to_datetime()
try:
start_dt_string = start_dt.strftime("%Y-%d-%m %H:%M:%S")
end_dt_string = end_dt.strftime("%Y-%d-%m %H:%M:%S")
start_ts = pd.Timestamp(start_dt_string)
end_ts = pd.Timestamp(end_dt_string)
df.loc[idx, "trip_start_time"] = start_ts
df.loc[idx, "trip_stop_time"] = end_ts
except ValueError:
pass
它有点慢(有一堆数据行)但我的计算机似乎正在处理它 - 如果失败会再次更新。
旧回复:所以,发生的事情是,每个不可能出现歧义的日期/时间都在原始数据集中,格式为:DD/MM/YYYY HH:MM:SS。
如果可以强制转换为 MM/DD/YY HH:MM:SS 那么它有
我会做的是遍历每一列
for row in df.index:
try:
new_dt = datetime.strptime(row, "%Y-%d-%m %H:%M:%S")
#write back to the df here
except ValueError:
pass#ignore anything that cannot be converted
关于python - 从 Excel 工作表导出后如何清理数据框中的日期时间字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500872/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!