Python 如何使用read_csv或lambda读取多个文件
我正在通过Python 如何使用read_csv或lambda读取多个文件,python,pandas,dataframe,lambda,Python,Pandas,Dataframe,Lambda,我正在通过read\u csv读取同一文本文件两次。第一次获取与该文件中的“Col6”匹配的特定字符串(MSG)的键列表。这将给我一个数据框,其中只包含与“Col6”匹配的条目。然后,第二次我读取相同的文件(再次使用read_csv),如果key1==key2,则打印更多基于“Col1”的列 我基本上有两个问题: 1.我可以将两个搜索(read\u csv)组合在一起吗? 2.即使我将这两个read_csv分开,我如何读取多个文件?现在我只读取一个文件(firstFile.txt),但我想用'*
read\u csv
读取同一文本文件两次。第一次获取与该文件中的“Col6”匹配的特定字符串(MSG)的键列表。这将给我一个数据框,其中只包含与“Col6”匹配的条目。然后,第二次我读取相同的文件(再次使用read_csv
),如果key1==key2
,则打印更多基于“Col1”的列
我基本上有两个问题:
1.我可以将两个搜索(read\u csv
)组合在一起吗?
2.即使我将这两个read_csv
分开,我如何读取多个文件?现在我只读取一个文件(firstFile.txt
),但我想用'*.txt'
替换文件名,以便对目录中的所有*.txt
文件执行读取csv
操作
数据文件如下所示。我想用Col1=12345
打印所有行,因为Col6
具有值“这是一个测试”
我使用的脚本是:
import csv
import pandas as pd
import os
import glob
DL_fields1 = ['Col1', 'Col2']
DL_fields2 = ['Col1', 'Col2','Col3', 'Col4', 'Col5', 'Col6']
MSG = 'This is a test'
iter_csv = pd.read_csv('firstFile.txt', chunksize=1000, usecols=DL_fields1, skiprows=1)
df = pd.concat([chunk[chunk['Special_message'] == MSG] for chunk in iter_csv])
for i, row in df.iterrows():
key1 = df.loc[i, 'Col1']
j=0
for line in pd.read_csv('firstFile.txt', chunksize=1, usecols=DL_fields2, skiprows=1, na_values={'a':'Int64'}):
key2 = line.loc[j,'Col1']
j = j + 1
if (key2 == '-'):
continue
elif (int(key1) == int(key2)):
print (line)
据我所知,您不需要读取CSV文件两次。实际上,您需要
MSG
出现在Col6
中的所有行。实际上,你可以在一行中实现这一点-
MSG = 'This is a test'
iter_csv = pd.read_csv('firstFile.txt', chunksize=1000, usecols=DL_fields1, skiprows=1)
# this gives you all the rows where MSG occurs in Col6
df = iter_csv.loc[iter_csv['Col6'] == MSG, :]
# this gives you all the rows where 12345 in Col1
df_12345 = df.loc[iter_csv['Col1'] == 12345,]
您可以通过这种方式创建多个数据子集
要回答问题的第二部分,您可以循环浏览所有文本文件,如下所示-
import glob
txt_files = glob.glob("test/*.txt")
for file in txt_files:
with open(file, 'r') as foo:
some_df = pd.read_csv(file)
编辑:这是您循环文件并使用
Col1=12345
和Col6=MSG
查找所有键的方式
import glob
from functools import reduce
results_list = []
MSG = 'This is a test'
txt_files = glob.glob("test/*.txt")
for file in txt_files:
with open(file, 'r') as foo:
some_df = pd.read_csv(file, chunksize=1000, usecols=DL_fields1, skiprows=1)
df = iter_csv.loc[iter_csv['Col6'] == MSG, :]
# results_list is a list of all such dataframes
results_list.append(df.loc[iter_csv['Col1'] == 12345, ])
# All results in one big dataframe
result_df = reduce(lambda x,y: pd.concat([x,y]), results_list)
据我所知,您不需要读取CSV文件两次。实际上,您需要
MSG
出现在Col6
中的所有行。实际上,你可以在一行中实现这一点-
MSG = 'This is a test'
iter_csv = pd.read_csv('firstFile.txt', chunksize=1000, usecols=DL_fields1, skiprows=1)
# this gives you all the rows where MSG occurs in Col6
df = iter_csv.loc[iter_csv['Col6'] == MSG, :]
# this gives you all the rows where 12345 in Col1
df_12345 = df.loc[iter_csv['Col1'] == 12345,]
您可以通过这种方式创建多个数据子集
要回答问题的第二部分,您可以循环浏览所有文本文件,如下所示-
import glob
txt_files = glob.glob("test/*.txt")
for file in txt_files:
with open(file, 'r') as foo:
some_df = pd.read_csv(file)
编辑:这是您循环文件并使用
Col1=12345
和Col6=MSG
查找所有键的方式
import glob
from functools import reduce
results_list = []
MSG = 'This is a test'
txt_files = glob.glob("test/*.txt")
for file in txt_files:
with open(file, 'r') as foo:
some_df = pd.read_csv(file, chunksize=1000, usecols=DL_fields1, skiprows=1)
df = iter_csv.loc[iter_csv['Col6'] == MSG, :]
# results_list is a list of all such dataframes
results_list.append(df.loc[iter_csv['Col1'] == 12345, ])
# All results in one big dataframe
result_df = reduce(lambda x,y: pd.concat([x,y]), results_list)
我认为你应该在一开始就阅读整个CSV。使用pandas操作过滤出后面需要的行。再次阅读同一个文件没有多大意义,一行一行地读。我不知道你到底在找什么,你能进一步澄清吗?我想要以下输出:12345 345 456 789 1011“这是一个测试”12345 365 856 789 1020-12345 345 456 789 1011-12345 345 456 789 1011-12345 365 856 789 1020-12345 385 956 689 1043-12345 385 556 889 1055-12345 345 456 789 1011-我认为您应该在开始时简单地阅读整个CSV。使用pandas操作过滤出后面需要的行。再次阅读同一个文件没有多大意义,一行一行地读。我不知道你到底在找什么,你能进一步澄清吗?我想要以下输出:12345 345 456 789 1011“这是一个测试”12345 365 856 789 1020-12345 345 456 789 1011-12345 345 456 789 1011-12345 365 856 789 1020-12345 385 956 689 1043-12345 385 556 889 1055-12345 345 456 789 1011-如果此解决方案适合您,请随时接受此答案。您建议的以下代码是否可以放入循环中,以便对多个文件而不是一个文件执行?我的意思是我可以用“*.txt”替换“firstFile.txt”吗?MSG='这是一个测试'iter_csv=pd.read_csv('firstFile.txt',chunksize=1000,usecols=DL_fields1,skiprows=1)#这提供了在Col6中出现MSG的所有行df=iter_csv.loc[iter_csv['Col6']=MSG,:]#这提供了Col1中12345=df.loc[iter_csv['Col1'=12345]的所有行如果此解决方案适合您,请随时接受此答案。您建议的以下代码是否可以放入循环中,以便对多个文件而不是一个文件执行?我的意思是我可以用“*.txt”替换“firstFile.txt”吗?MSG='这是一个测试'iter_csv=pd.read_csv('firstFile.txt',chunksize=1000,usecols=DL_fields1,skiprows=1)#这提供了在Col6中出现MSG的所有行df=iter_csv.loc[iter_csv['Col6']=MSG,:]#这提供了Col1中12345=df.loc[iter_csv['Col1'=12345]的所有行