【问题标题】:Pandas: Retain the column value based on the value present in dictionary and make other columns as blankPandas:根据字典中存在的值保留列值,并将其他列设为空白
【发布时间】:2021-06-28 03:27:23
【问题描述】:

我有一个数据框

df = pd.DataFrame([["A","X",98,56,1,2,3,4], ["B","Z",79,54,36,3,4,8], ["C","Y",98,56,2,5,6,7],["A","Y",79,54,36,12,13,24], ["B","X",98,56,3,6,7,8], ["C","Z",48,51,85,5,6,5]], columns=["id","key","c1","c2","c3","c4","C5","C6"])

我有一本字典

dic = {"X":['c1','c3'],"Y":['c2','c4'],"Z":['c5','c6']}

根据df的键列,使用字典dic选择列,只保留那些列中的行值,其他行值留空。

例如:对于 df 的键 X,将 C1 和 C3 中的值保持在字典中,并将其他列留空。

预期输出:

df_out = pd.DataFrame([["A","X",98,"",1,"","",""], ["B","Z","","","","",4,8], ["C","Y","",56,"",5,"",""],["A","Y","",54,"",12,"",""], ["B","X",98,"",3,"","",""], ["C","Z","","","","",6,5]], columns=["id","key","c1","c2","c3","c4","C5","C6"])

怎么做?

【问题讨论】:

  • 很好的问题。 +1。

标签: python python-3.x pandas python-2.7 dataframe


【解决方案1】:

对不匹配的列使用Index.difference,并在DataFrame.loc中设置空字符串:

dic = {"X":['c1','c3'],"Y":['c2','c4'],"Z":['C5','C6']}

for k, v in dic.items():
    df.loc[df.key == k, df.columns.difference(v + ['id', 'key'])] = ''

print (df)
  id key  c1  c2 c3  c4 C5 C6
0  A   X  98      1          
1  B   Z                 4  8
2  C   Y      56      5      
3  A   Y      54     12      
4  B   X  98      3          
5  C   Z                 6  5

【讨论】:

  • 非常好的解决方案。 +1
猜你喜欢
  • 1970-01-01
  • 2019-08-02
  • 2018-07-09
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
相关资源
最近更新 更多