简体   繁体   English

如果列名与另一个 DataFrame pandas 的行值匹配,则获取 DataFrame 的列值

[英]Get column values of a DataFrame if column name matches row value of another DataFrame pandas

I have two DataFrame,我有两个 DataFrame,

A = {'col1': [n, b], 'col2': [c,a]}
B = {'a': [1, 24, 30], 'b': [100, nan, 10],'c': [nan, 4.6, nan],'n': [10, 2, 98.2] }
df_a = pd.DataFrame(data=a)
df_b = pd.DataFrame(data=b)

what I'm trying to do is iterating over df_a rows, first considering col1 and then col2, and each time a row is equal to a column name of df_b, I want to retrieve the values under that column.我想要做的是遍历 df_a 行,首先考虑 col1 然后 col2,每次一行等于 df_b 的列名时,我想检索该列下的值。

For example, if in the first row of df_a (col1) is present “n” then I want to get a Dataframe that will have 10,2,98.2 as rows.例如,如果 df_a (col1) 的第一行中存在“n”,那么我想获得一个 Dataframe,其中行数为 10,2,98.2。 And then move to col2 to do the same.然后移动到 col2 做同样的事情。 At the end, for each iteration I'd like to have a new Dataframe with two columns each.最后,对于每次迭代,我都希望有一个新的 Dataframe,每个都有两列。 In the example, the first iteration would give me a DataFrame like this:在示例中,第一次迭代会给我一个 DataFrame ,如下所示:

     n      b 
0   10    100
1   2     nan
2   98.2   10

I tried with this, but without success:我试过这个,但没有成功:

if row['col1'] == df_b.columns.any():
     values_df = df_a['col1'].values[:]

This is not a perfect solution and I violated many zen rules here but it still works I hope:这不是一个完美的解决方案,我在这里违反了许多禅宗规则,但我希望它仍然有效:

df = pd.DataFrame()
for col in df_a.columns:
    new_df = pd.concat(df_a[col].apply(lambda x: pd.DataFrame({x: df_b[x]}) if x in df_b.columns else None).values, axis=1)
    df[[col for col in new_df.columns]] = new_df

You could write a function to take a list of column names and return the Pandas DataFrame based on the values in respective columns of df_b like this:您可以编写一个 function 来获取列名列表并返回 Pandas DataFrame 基于df_b相应列中的值,如下所示:

def lookup_columns(lookups: list, values_df: pd.DataFrame) -> pd.DataFrame:
    result = {}
    
    for key in lookups:
        try:
            result[key] = list(values_df[key].values)
        except KeyError as err:
            pass
        
    return pd.DataFrame(result)

Since you said you want a DataFrame from each iteration, you could iterate over the columns of df_a and create a dictionary of Pandas DataFrames like this:既然你说你想要一个 DataFrame 从每次迭代中,你可以迭代df_a的列并创建一个 Pandas DataFrames 的字典,如下所示:

mapped_columns = {}

for col in df_a.columns:
    mapped_columns[col] = lookup_columns(list(df_a[col].values), df_b)

Based on the code example you provided, you would end up with a dictionary with two entries:根据您提供的代码示例,您最终会得到一个包含两个条目的字典:

mapped_columns['col1']

    n       b
0   10.0    100.0
1   2.0 NaN
2   98.2    10.0

And

mapped_columns['col2']


    c   a
0   NaN 1
1   4.6 24
2   NaN 30

You can just use referencing on df_a columns.您可以只在df_a列上使用引用。

df_b[df_a['col1']]
Out[7]: 
      n      b
0  10.0  100.0
1   2.0    NaN
2  98.2   10.0

df_b[df_a['col2']]
Out[8]: 
     c   a
0  NaN   1
1  4.6  24
2  NaN  30

If you want to make iterations and assign it to new dataframe, you can use a list to store the dataframes.如果要进行迭代并将其分配给新的 dataframe,则可以使用列表来存储数据帧。

dataframe_list = []
for i in df_a:
    dataframe_list.append(df_b[df_a[i]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在行匹配条件的Pandas DataFrame中获取第一列值 - Get first column value in Pandas DataFrame where row matches condition 获取列值与列表匹配的数据框中的每一行:Pandas - Get every row in a dataframe whose column value matches a list: Pandas Pandas 将列的所有值与不同的 DataFrame 进行比较,并返回值匹配的列名(不同的 DataFrame) - Pandas compare all values of a column with different DataFrame and return column name (of a dif. DataFrame) where value matches 如果数据框中的另一列使用pandas匹配某个值,则从数据框中的列中减去值 - substract values from column in dataframe if another column in dataframe matches some value using pandas 如果“行”,“列”中的值与另一列中的任何地方匹配,则删除“熊猫数据框”中的行 - Delete Row in Pandas Dataframe if value in Row, Column Matches Anywhere in Another Column 如果行名存在于熊猫中另一个数据框的列名中,则选择数据框的行值 - Select the row values of dataframe if row name is present in column name of another dataframe in pandas 如何获取pandas DataFrame中第二大行值的列名 - How to get column name for second largest row value in pandas DataFrame Pandas 单元格值是另一个数据框中的列名 - Pandas cell value is a column name in another dataframe Append 如果某个列匹配,则 pandas 行包含来自另一行 dataframe 的数据 - Append a pandas row with data from another dataframe if a certain column matches 如果同一 dataframe 中的另一列符合条件,如何从 pandas 中的列获取值? - How to get values from a Column in pandas if another column in same dataframe matches a condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM