覆盖两个熊猫数据框或numpy数组并创建键值字典

我有两个形状的CSV文件,可以说3×3.

文件1如下所示:

-1, 2,-1
-1,-1, 3
-1, 8, 9

和文件2这样:

-1, 56,-1
-1,-1,  73
-1, 24, 100

我的目标是覆盖数组或DataFrames并创建以下形式的字典:

dict = {2:56, 3:73, 8:24, 9:100}

我可以创建基于循环的内容,但是我想知道是否存在一种掩盖或覆盖数组以及创建相应字典的方法?

解决方法:

您可以展平数据框,删除空值,然后对它们进行dict / zip:

import pandas as pd

# Load the data, converting -1 to NaN
dfa = pd.read_csv('file1.csv', header=None, na_values=[-1])
dfb = pd.read_csv('file2.csv', header=None, na_values=[-1])

# Flatten the dataframes
a = dfa.values.flatten()
b = dfb.values.flatten()

# Remove null values and cast back to ints (if that matters)
# Note that both are filtered according to the key data
a = a[~np.isnan(a)].astype(int)
b = b[~np.isnan(a)].astype(int)

# Zip to pair the lists, then convert to a dict
d = dict(zip(a, b))
上一篇:android-如何检测我的活动何时被遮盖?


下一篇:pygame 模块