小编cod*_*cer的帖子

从数据帧列表中获取列名和数据帧名称到单个数据帧中

我正在寻找一种方法来将数据框列表中的列名和数据框名称转换为单个数据框。它们的列长度不等。做到这一点的最佳方法是什么?

dlist <- list(mtcars[1:2], mtcars[1:3], mtcars[1:4])
names(dlist) <- c("mtcars1", "mtcars2", "mtcars3")

Tried:
dlist |> map(~colnames(.x))
Run Code Online (Sandbox Code Playgroud)

预期输出:

1 mtcars1 mpg   cyl   NA    NA   
2 mtcars2 mpg   cyl   disp  NA   
3 mtcars3 mpg   cyl   disp  hp  
Run Code Online (Sandbox Code Playgroud)

r dplyr purrr

8
推荐指数
2
解决办法
291
查看次数

在 Python Altair 中使条形图的 x 轴标记水平或 45 度可读

我正在尝试创建一个在 x 轴上包含年份数据的条形图。它可以工作,但是 x-xais 上的年份标记都是垂直方向,我想让它们更具可读性 - 水平或 45 度。我尝试使用year:T数据时间格式,但它给了我年份和月份标记(我只想在 xais 上有年份标记)。如何使 x 轴上的年份标记为水平或 45 度?

import altair as alt
from vega_datasets import data

source = data.wheat()
source.info()

abars = alt.Chart(source).mark_bar().encode(
    x='year:O',
    y="wheat:Q"
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

python altair

7
推荐指数
1
解决办法
4300
查看次数

将多个缺失值的列表转换为一个字典

我正在尝试将多个列表添加到字典中 - 一个键和一个值列表。我可以在字典中添加两个列表,但我想知道如何将多个列表与其中一个列表中的某些缺失值连接起来。

a = ['apple', 'orange', 'lemon']
b = ['London', 'New York', 'Tokyo']
c = ['Red', 'Orange', 'Yellow']
d = ['good', 'bad', '']

fruito = zip(a, b)
fruit_dictionary = dict(fruito); fruit_dictionary
Run Code Online (Sandbox Code Playgroud)

预期输出:

fruit_dictionary = {'apple': ['London', 'Red', 'good'],
                    'orange': ['New York', 'Orange', 'bad'],
                    'lemon': ['Tokyo', 'Yellow']}
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
1
解决办法
195
查看次数

Python 中重复的列范围乘法和除法

我正在尝试分组进行一些列范围的乘法和除法,并将它们连接在一起。但我想大规模地做到这一点 - 目前它有点重复 - 具有 A 字符串的列除以具有 D 字符串的列并乘以 WC 列,然后在组列 B 和 C 上向 D 列和 WC 列重复类似的过程。最后,我将它们合并到同一个数据框中。我怎样才能使这个过程更有效率?

输入:

df = pd.DataFrame({"cid" : {0 : "cd1", 1 : "cd2", 2 : "cd3"},
                   "A1970" : {0 : 3.2, 1 : 3.5, 2 : .4},
                   "A1980" : {0 : 3.1, 1 : 3.6, 2 : .5},
                   "B1970" : {0 : 2.5, 1 : 1.2, 2 : .7},
                   "B1980" : {0 : 3.2, 1 : 1.3, 2 : .1},
                   "C1970" : {0 …
Run Code Online (Sandbox Code Playgroud)

python numpy pandas

3
推荐指数
2
解决办法
116
查看次数

仅在 Polars 数据框中删除选定列上的空值

我正在尝试删除所选列列表中的空值。但看来我的操作可能with_columns不太对劲。如果您只想对选定的列进行删除操作,正确的方法是什么?

\n
df = pl.DataFrame(\n    {\n        "id": ["NY", "TK", "FD"], \n        "eat2000": [1, None, 3], \n        "eat2001": [-2, None, 4],\n        "eat2002": [None, None, None],\n        "eat2003": [-9, None, 8],\n        "eat2004": [None, None, 8]\n    }\n); df\n\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\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 id  \xe2\x94\x86 eat2000 \xe2\x94\x86 eat2001 \xe2\x94\x86 eat2002 \xe2\x94\x86 eat2003 \xe2\x94\x86 eat2004 \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 ---     \xe2\x94\x86 ---     \xe2\x94\x86 ---     \xe2\x94\x86 ---     \xe2\x94\x86 ---     \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 i64     \xe2\x94\x86 i64     \xe2\x94\x86 f64     \xe2\x94\x86 i64     \xe2\x94\x86 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\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\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 NY  \xe2\x94\x86 1       \xe2\x94\x86 -2      \xe2\x94\x86 null    \xe2\x94\x86 …
Run Code Online (Sandbox Code Playgroud)

python python-polars

2
推荐指数
1
解决办法
9530
查看次数

Pandas:在Python中合并两个字符串列,删除重复的字符串并删除不需要的字符串,除非只剩下不需要的字符串

我正在尝试合并两个字符串列,并且希望摆脱'others'计数器值是否为“非其他”值 - 例如'apple' + 'others' = 'apple'but 'others' + 'others' = 'others'。我管理了第二个条件,但是如何在合并时适应这两个条件?

data = {'fruit1':["organge", "apple", "organge", "organge", "others"],
        'fruit2':["apple", "others", "organge", "watermelon", "others"]}
df = pd.DataFrame(data)

df["together"] = df["fruit1"] + ' ' + df["fruit2"]
df["together"] = df["together"].apply(lambda x: ' '.join(pd.unique(x.split())))

    fruit1      fruit2            together
0  organge       apple       organge apple
1    apple      others        apple others
2  organge     organge             organge
3  organge  watermelon  organge watermelon
4   others      others              others
Run Code Online (Sandbox Code Playgroud)

预期输出:

    fruit1      fruit2            together
0  organge       apple       organge …
Run Code Online (Sandbox Code Playgroud)

python pandas

1
推荐指数
1
解决办法
737
查看次数

标签 统计

python ×5

pandas ×3

altair ×1

dplyr ×1

numpy ×1

purrr ×1

python-polars ×1

r ×1