Python 转换为数据帧的真/假值
我有一个相当大的数据框架,看起来有点像这样:Python 转换为数据帧的真/假值,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个相当大的数据框架,看起来有点像这样: | obj1 | obj2 | obj3 | |------------------------ 0 | attr1 | attr2 | attr1 | 1 | attr2 | attr3 | NaN | 2 | attr3 | attrN | NaN | | obj1 | obj2 | obj3 | ------------------------ attr1 | True | False | T
| obj1 | obj2 | obj3 |
|------------------------
0 | attr1 | attr2 | attr1 |
1 | attr2 | attr3 | NaN |
2 | attr3 | attrN | NaN |
| obj1 | obj2 | obj3 |
------------------------
attr1 | True | False | True |
attr2 | True | False | False |
attr3 | True | False | False |
我是熊猫的新手,但我想不出一个办法让它看起来像这样:
| obj1 | obj2 | obj3 |
|------------------------
0 | attr1 | attr2 | attr1 |
1 | attr2 | attr3 | NaN |
2 | attr3 | attrN | NaN |
| obj1 | obj2 | obj3 |
------------------------
attr1 | True | False | True |
attr2 | True | False | False |
attr3 | True | False | False |
解决这个问题最快的方法是什么
编辑
数据框中没有包含所有属性的列。
我可以有一个Obj4,它的属性在其他任何地方都看不到您需要+:
类似的解决方案:
df = df.set_index('obj1', drop=False).rename_axis(None)
df = df.eq(df.index.values, axis=0)
print (df)
obj1 obj2 obj3
attr1 True False True
attr2 True False False
attr3 True False False
df = pd.DataFrame(df.values == df['obj1'].values[:, None],
index=df['obj1'].values,
columns=df.columns)
print (df)
obj1 obj2 obj3
attr1 True False True
attr2 True False False
attr3 True False False
和numpy解决方案:
df = df.set_index('obj1', drop=False).rename_axis(None)
df = df.eq(df.index.values, axis=0)
print (df)
obj1 obj2 obj3
attr1 True False True
attr2 True False False
attr3 True False False
df = pd.DataFrame(df.values == df['obj1'].values[:, None],
index=df['obj1'].values,
columns=df.columns)
print (df)
obj1 obj2 obj3
attr1 True False True
attr2 True False False
attr3 True False False
编辑:
要比较所有值并不容易:
vals = df.stack().unique()
L = [pd.Series(df[x].unique(), index=df[x].unique()).reindex(index=vals) for x in df.columns]
df1 = pd.concat(L, axis=1, keys=df.columns)
print (df1)
obj1 obj2 obj3
attr1 attr1 NaN attr1
attr2 attr2 attr2 NaN
attr3 attr3 attr3 NaN
attrN NaN attrN NaN
df1 = df1.eq(df1.index.values, axis=0)
print (df1)
obj1 obj2 obj3
attr1 True False True
attr2 True True False
attr3 True True False
attrN False True False
编辑1:
df1的另一个解决方案是:
stacked = df.stack()
#reshape to MultiIndex
df1 = stacked.reset_index(name='A').set_index(['level_1','A'])
#MultiIndex with all possible values
mux = pd.MultiIndex.from_product([df1.index.levels[0], stacked.unique()])
#reindex by MultiIndex
df1 = df1.reindex(index=mux)
#replace non NaN values to second level of MultiIndex
df1['level_0'] = df1['level_0'].mask(df1['level_0'].notnull(),
df1.index.get_level_values(1))
#reshape back
df1 = df1['level_0'].unstack(0)
print (df1)
obj1 obj2 obj3
attr1 attr1 NaN attr1
attr2 attr2 attr2 NaN
attr3 attr3 attr3 NaN
attrN NaN attrN NaN
这看起来是对的,但是如果我没有具有所有属性的对象呢?附加信息:我从defaultdict(列表)创建了这个数据帧。您可以为它添加示例吗?你认为你不需要与第一列进行比较吗?我没有一列可以比较,我的想法是从整个数据集中取所有不同的值,并与每个系列进行比较。为每个objectOk创建一个包含属性和true或false值的索引列,因此如果唯一值为
['attr1''attr2''attr3''attrN''attr4']
您的输入数据框需要输出什么?非常感谢您的接受。这个问题真的不容易。您也可以向上投票-单击接受标记上方的4
小三角形。谢谢