[英]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.