pandasdf=pd.DataFrame(
{
"A": [1, 2, 3, 4, 5],
"fruits": ["banana", "banana", "apple", "apple", "banana"],
"B": [5, 4, 3, 2, 1],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
"optional": [28, 300, None, 2, -30],
}
)
pandasdf.groupby(["fruits","cars"])['B'].sum().unstack()
Run Code Online (Sandbox Code Playgroud)
如何在极坐标中创建等效的真值表?
类似于下表的真值表
df=pl.DataFrame(
{
"A": [1, 2, 3, 4, 5],
"fruits": ["banana", "banana", "apple", "apple", "banana"],
"B": [5, 4, 3, 2, 1],
"cars": ["beetle", "audi", "beetle", "beetle", "beetle"],
"optional": [28, 300, None, 2, -30],
}
)
df.groupby(["fruits","cars"]).agg(pl.col('B').sum()) #->truthtable
Run Code Online (Sandbox Code Playgroud)
代码的效率很重要,因为数据集太大(与 apriori 算法一起使用)
Polars 中的 unstack 函数是不同的,pd.crosstab …
我想使用 tidyvers 将我的应用程序从 R 迁移到 Python Polars,这段代码在 python Polars 中的等价物是什么?
new_table <- table1 %>%
mutate(no = row_number()) %>%
mutate_at(vars(c, d), ~ifelse(no %in% c(2,5,7), replace_na(., 0), .)) %>%
mutate(e = table2$value[match(a, table2$id)],
f = ifelse(no %in% c(3,4), table3$value[match(b, table3$id)], f))
Run Code Online (Sandbox Code Playgroud)
我尝试查看用于组合数据和选择数据的极坐标文档,但仍然不明白
'有没有办法在 Polars 中复制 groupby -> 转换功能?
我用它来标准化组(使组总和为一),即
df['normalised'] = df.groupby[*groupcols*].transform(lambda x: x/x.sum())
我通过添加一列求和然后除以该列来解决这个问题:
df.join((df.groupby(by=[*groupcols*]).agg((pl.col('VOL').sum()).alias('VOLSUM'))),
left_on = [*groupcols*],
right_on = [*groupcols*]).with_column(
(pl.col('VOL') / pl.col('VOLSUM')).alias('VOLNORM')
).drop('VOLSUM')
Run Code Online (Sandbox Code Playgroud)
然而,我需要的另一个是组内的累计总和:
df['cummulativesum'] = df.groupby[*groupcols*][col].transform('cumsum')
向 Polars 人提两个问题:我对群体的标准化是最好的方法吗?有没有一种方法可以在极地组中进行相当于 cumsum 的极地?
提前致谢!
给定一个 Polars DataFrame,有没有办法用“null”替换特定值?例如,如果有一个像这样的哨兵值"_UNKNOWN",我想让它在数据框中真正缺失。
熊猫
df['sentences'] = df['content'].str.split(pattern2)
df['normal_text'] = df['sentences'].apply(lambda x: [re.sub(pattern3, ' ', sentence) for sentence in x])
Run Code Online (Sandbox Code Playgroud)
极地
df = df.with_column(pl.col('content').apply(lambda x: re.split(pattern2, x)).alias('sentences'))
df = df.with_column(pl.col('sentences').apply(lambda x: [re.sub(pattern3, ' ', sentence) for sentence in x]).alias('normal_text'))
Run Code Online (Sandbox Code Playgroud)
艾米比这更优雅吗?
说我有
\ndf = pl.DataFrame({\'group\': [1, 1, 1, 3, 3, 3, 4, 4], \'value\': [1, 4, 2, 5, 3, 4, 2, 3]})\nRun Code Online (Sandbox Code Playgroud)\n我想为每个组获取滚动总和,窗口为 2
\n预期输出是:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 value \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 1 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 5 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 6 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 5 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 8 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 7 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 2 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 5 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\nRun Code Online (Sandbox Code Playgroud)\n 当我将 parquet 文件加载到 Polars DataFrame 中时,需要大约 5.5 GB 的 RAM。与我尝试过的其他选择相比,Polars 非常棒。然而,Polars 不支持创建像 Pandas 这样的索引。这对我来说很麻烦,因为我的 DataFrame 中的一列是唯一的,并且在我的应用程序中访问 df 中的数据的模式是基于唯一列(类似字典)的行查找。
由于数据帧很大,过滤成本太高。不过,我的 RAM 似乎也不够(32 GB)。我目前正在将 df 转换为“块”,如下所示:
h = df.height # number of rows
chunk_size = 1_000_000 # split each rows
b = (np.linspace(1, math.ceil(h/chunk_size), num=math.ceil(h/chunk_size)))
new_col = (np.repeat(b, chunk_size))[:-( chunk_size - (h%chunk_size))]
df = df.with_column(polars.lit(new_col).alias('new_index'))
m = df.partition_by(groups="new_index", as_dict=True)
del df
gc.collect()
my_dict = {}
for key, value in list(m.items()):
my_dict.update(
{
uas: frame.select(polars.exclude("unique_col")).to_dicts()[0]
for uas, frame in
(
value
.drop("new_index")
.unique(subset=["unique_col"], …Run Code Online (Sandbox Code Playgroud) 所以我正在使用 Polars read_excel 函数,并且我正在从 Excel 文件中读取一些日期。但是,当我读入它们时,它们被格式化为格式为“mm-dd-yy”的字符串。这会导致问题,因为我在 Excel 文件中的日期为 01/01/1950(转换为“01-01-50”),但是当我使用由极地,我的代码认为日期是 01/01/2050,因为我没有引入全年。
您可以在下面的 print() 语句中看到,即使我提取 2050 年和 1950 年的日期,当使用极坐标引入时,它们在 DF 中看起来都是相同的日期。那么有没有办法引入全年值来防止这种情况并区分实际日期呢?
代码:
import polars as pl
extracted = pl.read_excel('file_name.xlsx')
print(extracted)
Run Code Online (Sandbox Code Playgroud)
文件名.xlsx:
打印(提取)=
Polars 是否具有像pandas.factorize一样将字符串列编码为整数 (1, 2, 3) 的功能?
在极地文档中没有找到它
我想用单个值替换极坐标 DataFrame 中的一行:
import numpy as np
import polars as pl
df = np.zeros(shape=(4, 4))
df = pl.DataFrame(df)
Run Code Online (Sandbox Code Playgroud)
例如,我想将索引 1 处的行中的所有值替换为 1.0 。
我一直在文档中寻找一种简单的解决方案,但找不到。
python-polars ×10
python ×7
analytics ×1
dataframe ×1
dictionary ×1
dplyr ×1
pandas ×1
parquet ×1
r ×1
tidyverse ×1