我在使用数据类型为列表的列(请将其称为column_1)上的pandas数据帧进行过滤时遇到一些问题。具体来说,我想只返回行,使column_1和另一个预定列表的交集不为空。但是,当我尝试将逻辑放在.where,function的参数中时,我总是会遇到错误。以下是我的尝试,并返回错误。
试图测试单个元素是否在列表中:
table[element in table['column_1']]
返回错误...
KeyError: False
尝试将列表与数据帧行中的所有列表进行比较:
table[[349569] == table.column_1]
返回错误Arrays were different lengths: 23041 vs 1
在测试两个列表的交集之前,我试图让这两个中间步骤失效。
感谢您花时间阅读我的问题!
答案 0 :(得分:0)
考虑try {
JSch jsch = new JSch();
jsch.setKnownHosts("~/.ssh/known_hosts");
Session session = jsch.getSession(
"my_login",
"my.host",
22);
session.setPassword(
"password");
// Autoadd system rsa-keys to system file like known_hosts by
// disabling strick keys checking:
java.util.Properties config = new java.util.Properties();
config.put(
"StrictHostKeyChecking",
"no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
System.out.println(
sftpChannel.ls("/"));
session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
pd.Series
s
测试清单s = pd.Series([[1, 2, 3], list('abcd'), [9, 8, 3], ['a', 4]])
print(s)
0 [1, 2, 3]
1 [a, b, c, d]
2 [9, 8, 3]
3 [a, 4]
dtype: object
test
应用test = ['b', 3, 4]
函数,将lambda
的每个元素转换为集合s
并intersection
test
要将其用作蒙版,请使用print(s.apply(lambda x: list(set(x).intersection(test))))
0 [3]
1 [b]
2 [3]
3 [4]
dtype: object
代替bool
list
答案 1 :(得分:0)
您可以长期使用,您可以将整个工作流程包装在函数中,并在需要的地方应用这些函数。由于您没有放置任何示例数据集。我以示例数据集为例进行解析。考虑到我有文本数据库。首先,我将在列表中找到#tags,然后搜索所需的唯一#tags并过滤数据。
# find all the tags in the message
def find_hashtags(post_msg):
combo = r'#\w+'
rx = re.compile(combo)
hash_tags = rx.findall(post_msg)
return hash_tags
# find the requered match according to a tag list and return true or false
def match_tags(tag_list, htag_list):
matched_items = bool(set(tag_list).intersection(htag_list))
return matched_items
test_data = [{'text': 'Head nipid mõnusateks sõitudeks kitsastel tänavatel. #TipStop'},
{'text': 'Homses Rooli Võimus uus #Peugeot208!\nVaata kindlasti.'},
{'text': 'Soovitame ennast tulevikuks ette valmistada, electric car sest uus #PeugeotE208 on peagi kohal! ⚡️⚡️\n#UnboringTheFuture'},
{'text': "Aeg on täiesti uueks roadtrip'i kogemuseks! \nLase ennast üllatada - #Peugeot5008!"},
{'text': 'Tõeline ikoon, mille stiil avaldab muljet läbi eco car, electric cars generatsioonide #Peugeot504!'}
]
test_df = pd.DataFrame(test_data)
# find all the hashtags
test_df["hashtags"] = test_df["text"].apply(lambda x: find_hashtags(x))
# the only hashtags we are interested
tag_search = ["#TipStop", "#Peugeot208"]
# match the tags in our list
test_df["tag_exist"] = test_df["hashtags"].apply(lambda x: match_tags(x, tag_search))
# filter the data
main_df = test_df[test_df.tag_exist]