Python: 如何在不丢失索引关联的情况下使用函数筛选pandas.Series?

2024-09-27 23:20:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas.DataFrame,我在上面迭代行。在每一行中,我需要过滤掉一些不值钱的值,并保持索引关联。这就是我现在的处境:

for i,row in df.iterrows():
    my_values = row["first_interesting_column":]
    # here I need to filter 'my_values' Series based on a function
    # what I'm doin right now is use the built-in python filter function, but what I get back is a list with no indexes anymore
    my_valuable_values = filter(lambda x: x != "-", my_values)

我怎么能做到呢?在


Tags: indataframepandasdfforismyfunction
2条回答

迭代行通常是不好的做法(而且非常慢)。正如@JohnE建议您使用applymap。在

如果我理解你的问题,我想你想做的是:

import pandas as pd
from io import StringIO

datastring = StringIO("""\
2009    2010    2011   2012
1       4       -      4
3       -       2      3
4       -       8      7
""")
df = pd.read_table(datastring, sep='\s\s+')
a = df[df.applymap(lambda x: x != '-')].astype(np.float).values
a[~np.isnan(a)]

IRC的一个家伙给了我答案。这里是:

w = my_values != "-" # creates a Series with a map of the stuff to be included/exluded
my_valuable_values = my_values[w]

。。。也可以缩短为。。。在

^{pr2}$

。。。当然,为了避免再走一步。。。在

row["first_interesting_column":][row["first_interesting_column":] != "-"]

相关问题 更多 >

    热门问题