判断表中的元素是否存在于另一个表 字符串中关键词交换
  X5zJxoD00Cah 2023年11月02日 67 0

判断表中的元素是否存在于另一个表

字符串中关键词交换

# https://blog.csdn.net/zx1245773445/article/details/101604495

list_test_1 = ['a','b','d']
list_test_2 = ['d','a','c']
seri_test_1 = pd.Series(list_test_1)
seri_test_2 = pd.Series(list_test_2)

'''test_1中的元素是否均存在于test_2'''
df_test_1_in_2 = seri_test_1.apply(lambda x: seri_test_2.str.contains(x).any()).to_frame(name= "test_1").query("test_1 == False")
if df_test_1_in_2.index.to_list(): # 是否 非空
    print("test_1中有元素不存在于test_2", seri_test_1.loc[df_test_1_in_2.index].values )
else: # not 非空
    print("test_1中的元素均存在于test_2")
# https://blog.csdn.net/weixin_39644021/article/details/110284908
# 只能完整替换replace 不能单元格内字符替换str.replace
# 只能采取中间变量的方法

'''字符串中元素互换'''
str_test = """
'''test_1中的元素是否均存在于test_2'''
df_test_1_in_2 = seri_test_1.apply(lambda x: seri_test_2.str.contains(x).any()).to_frame(name= "test_1").query("test_1 == False")
if df_test_1_in_2.index.to_list(): # 是否 非空
    print("test_1中有元素不存在于test_2", seri_test_1.loc[df_test_1_in_2.index].values )
else: # not 非空
    print("test_1中的元素均存在于test_2")
"""
str_test_mid = str_test.replace("test_1", "test_3")
str_test_exchange_1 = str_test_mid.replace("test_2", "test_1")
str_test_exchange_2 = str_test_exchange_1.replace("test_3", "test_2")
print(str_test_exchange_2)

# 函数化 二个变量交换 需要一个中间变量
def fun_exchange(string= "abab", arg1= "a", arg2= "b", arg3= "mid"):
    string_mid = string.replace(arg1, arg3)
    string_exchange_1 = string_mid.replace(arg2, arg1)
    string_exchange_2 = string_exchange_1.replace(arg3, arg2)
    return string_exchange_2
fun_exchange()

# n个变量交换 需要?个中间变量
# 输出异常表格内容

# https://blog.csdn.net/zx1245773445/article/details/101604495
list_test_1 = ['a','b','d','f']
list_test_2 = ['d','a','c']
seri_test_1 = pd.Series(list_test_1)
seri_test_2 = pd.Series(list_test_2)
df_test_1 = seri_test_1.to_frame(name= "col1").join(seri_test_2.to_frame(name= "col2"))


'''test_1中的元素是否均存在于test_2'''
df_test_1_in_2 = seri_test_1.apply(lambda x: seri_test_2.str.contains(x).any()).to_frame(name= "test_1").query("test_1 == False")
if df_test_1_in_2.index.to_list(): # 是否 非空
    print("test_1中有元素不存在于test_2:\n\n", df_test_1.loc[df_test_1_in_2.index] )
else: # not 非空
    print("test_1中的元素均存在于test_2")

df.join(含列名的series或df)

这段话写在代码块里被吞了=.=


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  X5zJxoD00Cah   2023年12月12日   17   0   0 Python.net
  X5zJxoD00Cah   2023年11月30日   20   0   0 GroupEmail字符串
X5zJxoD00Cah