如何选择包含特定字段数量的行,当特定列具有特定的分隔符时。

huangapple go评论98阅读模式
英文:

How to select the rows when the particular column has specific number of fields with delimiter

问题

以下是您要求的翻译:

  1. 我有以下数据框
  2. import pandas as pd
  3. import numpy as np
  4. d = {'Cell':['cell_D1_TY_L_90','cell4_D2_TY_L_90','cell6_TY_L_90','cell2_D4_TY_L_90','cell1_L_90'],'D1':[5, 2, 2, 6,6], 'D2':[np.nan, 5, 6, np.nan,3], 'D3':[7,np.nan, 5, 5,np.nan], 'D6':[17, 3, np.nan,np.nan,2],'diff%':[np.nan,['D2'],['D2','D3'],['D1','D3'],['D1','D2','D6']]}
  5. df = pd.DataFrame(d)
  6. Cell D1 D2 D3 D6 diff%
  7. 0 cell_D1_TY_L_90 5 NaN 7.0 17.0 NaN
  8. 1 cell4_D2_TY_L_90 2 5.0 NaN 3.0 [D2]
  9. 2 cell6_TY_L_90 2 6.0 5.0 NaN [D2,D3]
  10. 3 cell2_D4_TY_L_90 6 NaN 5.0 NaN [D1, D3]
  11. 4 cell1_L_90 6 3.0 NaN 2.0 [D1, D2, D6]
  12. 我想要创建2件事
  13. **1** . 通过获取'diff%'列中指定的列名来为数据框设置样式并将相应的列值加粗并更改文本颜色为红色
  14. 例如考虑第3
  15. Cell D1 D2 D3 D6 diff%
  16. 3 cell2_D4_TY_L_90 **6** NaN **5.0** NaN [D1, D3]
  17. 我希望将D1D3的值 --> 6 5.0 加粗显示为红色
  18. **2** . 创建以下2个数据框
  19. 1.'Cell'列中恰好有5个字段的数据框采用上述样式
  20. 2.'Cell'列中少于5个字段的数据框采用上述样式
  21. Cell D1 D2 D3 D6 diff%
  22. 0 cell_D1_TY_L_90 5 NaN 7.0 17.0 NaN
  23. 1 cell4_D2_TY_L_90 2 5.0 NaN 3.0 [D2]
  24. 3 cell2_D4_TY_L_90 6 NaN 5.0 NaN [D1, D3]
  25. Cell D1 D2 D3 D6 diff%
  26. 2 cell6_TY_L_90 2 6.0 5.0 NaN [D2,D3]
  27. 4 cell1_L_90 6 3.0 NaN 2.0 [D1, D2, D6]
  28. 请告诉我是否有解决方案
英文:

I have the below dataframe:

  1. import pandas as pd
  2. import numpy as np
  3. d = {'Cell':['cell_D1_TY_L_90','cell4_D2_TY_L_90','cell6_TY_L_90','cell2_D4_TY_L_90','cell1_L_90'],'D1':[5, 2, 2, 6,6], 'D2':[np.nan, 5, 6, np.nan,3], 'D3':[7,np.nan, 5, 5,np.nan], 'D6':[17, 3, np.nan,np.nan,2],'diff%':[np.nan,['D2'],['D2','D3'],['D1','D3'],['D1','D2','D6']]}
  4. df = pd.DataFrame(d)
  5. Cell D1 D2 D3 D6 diff%
  6. 0 cell_D1_TY_L_90 5 NaN 7.0 17.0 NaN
  7. 1 cell4_D2_TY_L_90 2 5.0 NaN 3.0 [D2]
  8. 2 cell6_TY_L_90 2 6.0 5.0 NaN [D2,D3]
  9. 3 cell2_D4_TY_L_90 6 NaN 5.0 NaN [D1, D3]
  10. 4 cell1_L_90 6 3.0 NaN 2.0 [D1, D2, D6]

I want to create 2 things.

1 . Style the dataframe by taking 'diff%' column specified column names & make the corresponding column value to bold and change text color to red

Eg. Consider row 3

  1. Cell D1 D2 D3 D6 diff%
  2. 3 cell2_D4_TY_L_90 **6** NaN **5.0** NaN [D1, D3]

I want to make D1 and D3 values --> 6 and 5.0 to bold red color.

2 . Create below 2 dataframes

  1. 1.dataframe with exactly 5 fields in 'Cell' column with above styling.
  2. 2.dataframe with less than 5 fields in the 'Cell' column with above styling.
  3. Cell D1 D2 D3 D6 diff%
  4. 0 cell_D1_TY_L_90 5 NaN 7.0 17.0 NaN
  5. 1 cell4_D2_TY_L_90 2 5.0 NaN 3.0 [D2]
  6. 3 cell2_D4_TY_L_90 6 NaN 5.0 NaN [D1, D3]
  7. Cell D1 D2 D3 D6 diff%
  8. 2 cell6_TY_L_90 2 6.0 5.0 NaN [D2,D3]
  9. 4 cell1_L_90 6 3.0 NaN 2.0 [D1, D2, D6]

Please let me know any solution to this?

答案1

得分: 1

你可以使用以下代码:

  1. def style(x):
  2. if isinstance(x['diff%'], list):
  3. return pd.Series('color: red; font-weight: bold', index=x['diff%']).reindex(x.index)
  4. else:
  5. return pd.Series(index=x.index)
  6. df.style.apply(style, axis=1)

输出结果请参考以下链接:
如何选择包含特定字段数量的行,当特定列具有特定的分隔符时。

使用的输入数据如下:

  1. d = {'Cell':['cell_D1_TY_L_90','cell4_D2_TY_L_90','cell6_TY_L_90','cell2_D4_TY_L_90','cell1_L_90'],
  2. 'D1':[5, 2, 2, 6, 6], 'D2':[np.nan, 5, 6, np.nan, 3], 'D3':[7, np.nan, 5, 5, np.nan],
  3. 'D6':[17, 3, np.nan, np.nan, 2],
  4. 'diff%':[np.nan, ['D2'], ['D2', 'D3'], ['D1', 'D3'], ['D1', 'D2', 'D6']]}
  5. df = pd.DataFrame(d)
英文:

You can use:

  1. def style(x):
  2. if isinstance(x['diff%'], list):
  3. return pd.Series('color: red; font-weight: bold',
  4. index=x['diff%']).reindex(x.index)
  5. else:
  6. return pd.Series(index=x.index)
  7. df.style.apply(style, axis=1)

Output:

如何选择包含特定字段数量的行,当特定列具有特定的分隔符时。

Used input:

  1. d = {'Cell':['cell_D1_TY_L_90','cell4_D2_TY_L_90','cell6_TY_L_90','cell2_D4_TY_L_90','cell1_L_90'],
  2. 'D1':[5, 2, 2, 6,6], 'D2':[np.nan, 5, 6, np.nan,3], 'D3':[7,np.nan, 5, 5,np.nan],
  3. 'D6':[17, 3, np.nan,np.nan,2],
  4. 'diff%':[np.nan,['D2'],['D2', 'D3'],['D1', 'D3'],['D1', 'D2', 'D6']]}
  5. df = pd.DataFrame(d)

huangapple
  • 本文由 发表于 2023年2月26日 21:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定