簡體   English   中英

如何在 CSV 文件中選擇最后一個原始文件並導入 Excel?

[英]how do i select last raw in CSV files and import in to Excel?

我如何使用 for 選擇文本文件中的最后一個原始文件? 這是我的第一個想法代碼:

import glob
import pandas as pd
path = input("Insert location:")
file_list = glob.glob(path + "/*.txt")
txt_list = []
for file in file_list:
    txt_list.append(pd.read_csv(file))
    for file in file_list:
        txt_list[-7::3]
excl_merged = pd.concat(txt_list, ignore_index=True)
excl_merged.to_excel('Total.xlsx', index=False) ]

您的代碼不正確。 這是一個應該可以工作的版本:

import glob
import pandas as pd
path = input("Insert location:")
file_list = glob.glob(path + "/*.txt")
df_list = []
for file in file_list:
    df = pd.read_csv(file)
    df_list.append(df.tail(3)) # last 3 rows from each file dataframe
excl_merged = pd.concat(df_list, ignore_index=True)
excl_merged.to_excel('Total.xlsx', index=False)

說明 tail()方法從數據幀中獲取最后幾行(作為參數提供)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM