我正在开发一个代码,用于在 for 循环中处理多个 .csv 文件,然后仅提取(到新的 .csv 文件中)与名为“20210-2.0”的特定列中的非空字符串单元格匹配的行。非空字符串单元格的名称相同(即 20210-2.0)。以下是显示 csv 文件部分内容的屏幕截图:
import pandas as pd
import glob
import os
path = './'
all_files = glob.glob(path + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None)
li.append(df)
df = li[li['20201-2.0'].notnull()]
print('extracting info from cvs...')
print(df)
# You can now export all outcomes in new csv files
file_name = filename + 'new' + '.csv'
save_path = os.path.abspath(
os.path.join(
path, file_name
)
)
print('saving ...')
export_csv = df.to_csv(save_path, index=None)
我收到以下错误:
df = li[li['20201-2.0'].notnull()]
TypeError: list indices must be integers or slices, not str
最佳答案
在循环内,读取文件后,自动过滤它,然后将其存储在列表中。
df = pd.read_csv(filename, index_col=None, header = 0) # You read the file in your directory under the variable filename, but it needs to know that you have a column header. Your '20201-2.0' value is a column name right?
df = df[df['20201-2.0'].notnull()] # You now get a new dataframe from the one you load, but now you only got the rows in which the column named '20201-2.0' has been populated.
li.append(df) # Store that dataframe in a list called `li`
我还注意到,在保存为新的 csv 文件时,您在每个 filname 中添加了
你拥有的字符串变量。 "new"
字符串和 ".csv"
字符串
你运行过这段代码吗?它不会将您的文件保存为“something.csvnew.csv”
吗?
关于python - 连续处理 .csv 文件并在特定列包含非空单元格时提取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413122/