简体   繁体   English

如何在 ndarray 中找到索引的值并转换为 pandas Dataframe?

[英]How do I find the value of an index in an ndarray and convert to pandas Dataframe?

I have an ndarray created by a cKDTree, like this:我有一个由ndarray创建的 ndarray,如下所示:

idx = array([[2941, 4837, 3593],
             [ 323, 3209, 3649]])

and I'd like to use that to create a pandas DataFrame using those as indices to another data frame that has some other symbols, for example:我想用它来创建一个 pandas DataFrame ,使用它们作为另一个具有其他符号的数据帧的索引,例如:

2941, A
4837, B
3593, C
323,  D
3209, E
3649, F

And, using something like gdf = pd.DataFrame(idx) I'd like to have a DataFrame而且,使用类似gdf = pd.DataFrame(idx)我想要一个DataFrame

    idx_0   idx_1   idx_2
0       A       B       C
1       D       E       F

instead of代替

    idx_0   idx_1   idx_2
0    2941    4837    3593
1     323    3209    3649

How do I do that with a multidimensional array?如何使用多维数组做到这一点? df.loc[idx] won't work. df.loc[idx]不起作用。

Use Series.map with apply for all columns of DataFrame :使用Series.mapapply DataFrame的所有列:

s = df.set_index('a')['b']
print (s)
a
2941    A
4837    B
3593    C
323     D
3209    E
3649    F
Name: b, dtype: object

idx = np.array([[2941, 4837, 3593],
             [ 323, 3209, 3649]])

gdf = pd.DataFrame(idx).apply(lambda x: x.map(s))
print (gdf)
   0  1  2
0  A  B  C
1  D  E  F

You could use applymap :您可以使用applymap

lookup = dict(zip(df[0], df[1]))
result = pd.DataFrame(idx).applymap(lookup.get)
print(result)

Output Output

     0   1   2
0    A   B   C
1    D   E   F

Assuming df is:假设df是:

      0    1
0  2941    A
1  4837    B
2  3593    C
3   323    D
4  3209    E
5  3649    F

As an alternative, given that idx is a numpy array, you could map using numpy.vectorize , and then build the DataFrame: As an alternative, given that idx is a numpy array, you could map using numpy.vectorize , and then build the DataFrame:

result = pd.DataFrame(np.vectorize(lookup.get)(idx))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM