当我有一个带有路径的Class 1: 73%
Shape of train: 33
Shape of test: 4
Class 1: 73%
Shape of train: 33
Shape of test: 4
Class 1: 73%
Shape of train: 33
Shape of test: 4
Class 1: 73%
Shape of train: 33
Shape of test: 4
...
时,我最终会做很多 $collection = User::with('city')
->withCount('userReferral')
->orderByDesc('user_referral_count')
->get();
$data = $collection->where('username', $userName);
或pd.DataFrame
例如:
.map(lambda path: Path(path).{method_name}
我觉得这很奇怪,特别是因为apply(axis=1)
确实实现了(
pd.DataFrame({'base_dir': ['dir_A', 'dir_B'], 'file_name': ['file_0', 'file_1']})
.assign(full_path=lambda df: df.apply(lambda row: Path(row.base_dir) / row.file_name, axis=1))
)
base_dir file_name full_path
0 dir_A file_0 dir_A/file_0
1 dir_B file_1 dir_B/file_1
,使得pathlib
之类的东西更加Python化和自然。
我没有在熊猫中实现任何/
类型,我缺少什么吗?
我发现最好一次完成一次df.base_dir / df.file_name
的操作,然后至少对与path
的路径连接进行矢量化:
astype(path)
答案 0 :(得分:1)
似乎最简单的方法是:
df.base_dir.map(Path) / df.file_name.map(Path)
它节省了对lambda函数的需要,但是您仍然需要映射到'Path'。
或者,只需:
df.base_dir.str.cat(df.file_name, sep="/")
后者在Windows上无法运行(谁在乎,对吗?:),但运行速度可能更快。
答案 1 :(得分:0)
import pandas as pd
import os
df = pd.DataFrame({"p1":["path1"],"p2":["path2"]})
df.apply(lambda x:os.path.join(x.p1, x.p2), axis=1)
输出:
0 path1\path2
dtype: object
编辑:
在被告知不使用分配后,您可以尝试
import os
import pandas as pd
df = pd.DataFrame({"p1":["path1", "path3"],"p2":["path2", "path4"]})
print(df.to_json(orient="values"))
输出
[["path1","path2"],["path3","path4"]]
从这里开始很简单,只需使用map(lambda x:os.path.join(*x), ...)
,您就会获得路径列表。
答案 2 :(得分:0)
pandas-path
pandas-path
封装了您需要的功能等。只需通过导入,它就会向 .path
和 pd.Series
添加一个 pd.Index
访问器,使 pathlib
方法可用。
import pandas as pd
import pandas_path
df = pd.DataFrame({'base_dir': ['dir_A', 'dir_B'], 'file_name': ['file_0', 'file_1']})
# .path accessor added by importing pandas_path
df.base_dir.path / df.file_name.path
#> 0 dir_A/file_0
#> 1 dir_B/file_1
#> dtype: object
创建于 2021-03-06 18:09:44 PST,由 reprexlite v0.4.2