- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 data.frame 或 tibble,它在一个脚本中写入 CSV 文件。在另一个脚本中,相同的 CSV 文件被读入 data.frame 或 tibble。使用 read_csv()
, 与 col_types=
参数,我可以指定要读入的列类型。这是一个例子:
# Create an example dataframe
df <- tibble::tibble(a=1L
, b=1.0
, c="a"
, d=TRUE
, e=lubridate::ymd_hms("2019-03-19T13:15:18Z")
, f=lubridate::ymd("2019-03-19")
, g=factor("a"))
# Write csv to file
readr::write_csv(df, "temp.csv")
# read it back in, supplying a col_types string spec
readr::read_csv("temp.csv", col_types="idclTDf")
#> # A tibble: 1 x 7
#> a b c d e f g
#> <int> <dbl> <chr> <lgl> <dttm> <date> <fct>
#> 1 1 1 a TRUE 2019-03-19 13:15:18 2019-03-19 a
col_types=
read_csv()
上的参数函数(或者让它猜测,我不想这样做)。我想要的是某种方式来获取原始
df
并且,在我写出来之前,生成
col_types
来自
df
的字符串可用于重新读取转储的 CSV 的对象。也就是说,我想要一些可以创建
"idclTDf"
的对象。字符串给定 data.frame 作为参数。
最佳答案
我确实有一个解决方案,它有效,但我认为它非常不完整且没有强化。这是我对解决方案的尝试。
# https://github.com/tidyverse/readr/issues/895
# Create function to take a tibble and return a character string that can be used in `readr::read_csv()`
# as the `col_types` argument to re-read this back into a dataframe after it had been written out
# by `write_csv()`.
get_col_types_short <- function(.df) {
# Get column classes from input dataframe
lst_col_classes__ <- purrr::map(.df, ~ class(.x))
# Map classes to known single-character col_types indicator
vl_col_class_char__ <- purrr::map_chr(lst_col_classes__, function(.e) {
dplyr::case_when(
"logical" %in% .e ~ "l"
, "integer" %in% .e ~ "i"
, "numeric" %in% .e ~ "d"
, "double" %in% .e ~ "d"
, "character" %in% .e ~ "c"
, "factor" %in% .e ~ "f"
, "Date" %in% .e ~ "D"
, "POSIXct" %in% .e ~ "T"
, TRUE ~ "c"
)
})
# Return vector of single-character col_type indicator.
# Element name is the source column it came from.
vl_col_class_char__
}
# Test it:
df <- tibble::tibble(a=1L
, b=1.0
, c="a"
, d=TRUE
, e=lubridate::ymd_hms("2019-03-19T13:15:18Z")
, f=lubridate::ymd("2019-03-19")
, g=factor("a"))
v__ <- get_col_types_short(df)
# Show what is actually returned
v__
#> a b c d e f g
#> "i" "d" "c" "l" "T" "D" "f"
# Collapse it to show how to use it
paste(v__, collapse="")
#> [1] "idclTDf"
# Write csv to file
readr::write_csv(df, "temp.csv")
# read it back in, using the above col_types string spec
readr::read_csv("temp.csv", col_types=paste(v__, collapse=""))
#> # A tibble: 1 x 7
#> a b c d e f g
#> <int> <dbl> <chr> <lgl> <dttm> <date> <fct>
#> 1 1 1 a TRUE 2019-03-19 13:15:18 2019-03-19 a
关于r - 基于现有数据帧为 read_csv 创建 col_types 字符串规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55249599/
我正在尝试使用 dask 读取 csv 文件,它给了我如下错误。但问题是我想要我的 ARTICLE_ID是 object(string) .谁能帮我成功读取数据? 回溯如下: ValueError:
我有一个文件,其中包含如下数据 1000000 183:0.6673;2:0.3535;359:0.304;363:0.1835 1000001 92:1.0 1000002 112:1.0 1000
我刚刚注意到 read_csv()以某种方式使用了意外的随机数(至少对我而言)。对应的基R函数read.csv()不这样做。那么,read_csv() 是什么意思?使用随机数?我查看了文档,但找不到明
我有一个 csv 文件,其中包含日期和时间戳作为两列。我正在使用 Pandas read_csv将内容读入数据帧。我的最终目标是根据数据绘制时间序列图。 !head vmstat.csv wait_p
我正在解析大量 csv 数据。数据相当“脏”,因为我有不一致的分隔符、虚假字符和格式问题,这些问题会导致 出现问题。 read_csv() . 然而,我的问题不是数据的肮脏,而是试图理解 的解析错误。
我有以下 csv: value value value value ... id 1 1 1
我无法读取仅知道部分文件名的 csv 文件。这是我所做的,但出现错误: "pandas.errors.ParserError: Error tokenizing data." What should
我想知道是否有办法在 read_csv() 过程中读取分类值。 通常您可以在事后进行转换,例如: df.zone = df.zone.astype('category') 此时 df 占用了更多内存,
我尝试用 pandas 分析的 HTTP 日志文件有时会有意外的行。这是我加载数据的方式: df = pd.read_csv('mylog.log', sep=r'\s(?=(
csv 数据示例: 2019-08-15 00:00:06,430 0:0 - {"info":{"name":"LTD - PUBLIC"}} pd.read_csv(filepath, sep=
我对 python 很陌生。 我正在尝试使用 python 自动执行一些建筑能耗数据的数据分析。我正在使用 python 2.7.3、pandas 0.12、Canopy with qtconsole
我有一个 csv 文件,格式如下: 0;2017-02-16 15:08:16;81,307877;;;;;;; 因此,分号作为分隔符,日期和时间以及带小数点逗号的 float 。最后是一堆分号。 P
我有一个 csv 文件如下: 0 5 1 10 2 15 3 20 4 25 我想将其保存为名称为 x,y 轴的数据框,然后绘制它。但是,当我分配 x,y 时,我得到了一个困惑的 DataFrame,
这里将更新最新的最全面的read_csv()函数功能以及参数介绍,参考资料来源于官网。 pandas库简介 官方网站里详细说明了pandas库的安装以及使用方法,在这里获取最新的pandas库信
从windows操作系统本地读取csv文件报错 ? 1
pandas.read_csv()遇到读进来乱码问题 1.设置encoding='gbk'或者encoding='utf-8'。pandas.read_csv('data.csv',encodin
我有一个包含日期、国家/地区(只能在预定义列表中取值)等信息的 csv 文件,我目前正在使用 converters 参数来进行一些完整性检查,如下所示: import pandas as pd def
我是 Pandas 库的新手。 我共享了基于数据框的代码。 有没有办法在没有任何定界符的情况下逐行读取 gzip 文件(使用整行,该行可以包含逗号和其他字符)作为单行并在数据框中使用它?似乎你必须提供
我正在有效地尝试将以下文件读入 DataFrame 并再次将其原封不动地写出。 F1,F2,F3,F4,F5 "blah","blah",123,"123","123" "blue","blue",4
我正在尝试读取一个在每个单元格中包含多个值的 csv 文件,并且我想将它们编码为单个 int 格式的字节以存储在 Pandas 单元格中,(例如 (1, 1) -> 771)。为此,我想使用 read
我是一名优秀的程序员,十分优秀!