Python Pandas在列表中查找元素

时间:2016-12-09 22:51:19

标签: python list pandas

我从pandas dataframe创建了两个列表,如下所示: -

List1 :是从文件夹中的多个csv文件创建的,如下所示...

import glob as gb
csv_files = gb.glob("csv\\*.csv")

# csv_files = csv_files[0:]

# Create list of the CSV in dataframes
dfList = []
for file in csv_files:
    df_csv = pd.read_csv(file, header=None)
    dfList.append(df_csv)    

# print (dfList)

# Convert each dataframe within the list into individual list
new_dfList = []
for ind_df in dfList:
    # Convert the dataframe to a list
    new_convert = ind_df.values.tolist()
    new_dfList.append(new_convert)

List2 :是根据包含关键字的文本文件创建的。

# Open keywords TEXT file
keywords = pd.read_csv("words.txt", header=None)

# Convert the dataframe to a list
keywords = keywords.values.tolist()

# Move out from inner list
# keywords = list(chain(*keywords))

# print (keywords)
keywords[0]

list2中的这些关键字将与list1中的每个列表进行比较。我该怎么做?

如何使第一个关键字(在list2中)转到第一个列表(在list1中),第二个关键字(在list2中)转到第二个列表(在list1中),第三个关键字(在list2中)转到第三个列表(在list1中)。 ...等等以搜索并返回匹配结果?

1 个答案:

答案 0 :(得分:0)

为什么不在熊猫中做这件事?

keywords = pd.read_csv("words.txt", header=None)

dfList = []
for file in csv_files:
    df_csv = pd.read_csv(file, header=None)
    dfList.append(df_csv)

df = pd.concat(dfList, keys=keywords)
# Or, make a panel
wp = pd.Panel(dict(zip(keywords, dfList)))
# Or, just leave it as a dictionary
df_dict = dict(zip(keywords, dfList))

如果你真的想要一份列表字典:

df_dict_list = dict(zip(keywords, [df.tolist() for df in dfList]))