如何制作 DataFrame,然后一次制作更多行?
import polars as pl
df_polars = pl.DataFrame({
'abc': [],
'def': [],
'ghi': []
})
value_to_set = 10.0
df_polars = df_polars.extend([
pl.lit(None).alias('abc'),
pl.lit(value_to_set).alias('def'),
pl.lit(None).alias('ghi')
])
print(df_polars)
它一次仅适用于一个字段。向数据框中添加多行的更有效方法是什么?
最佳答案
集思广益=>添加字典列表,转换为DF,并连接:
new_rows = [{'abc': None, 'def': value, 'ghi': None} for value in values_to_set] # list of the dictionary
new_df = pl.DataFrame(new_rows) # new DataFrame with the new rows
df_polars = pl.concat([df_polars, new_df]) # Concatenate
关于python - 通过一次附加多行创建 Polars 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77307743/