[英]Pandas Concatenate dataframes
這讓我發瘋! 我有幾個數據框試圖與熊貓連接。 索引是文件名。 當我將 df.to_csv 用於單個數據框時,我可以看到索引列(文件名)以及感興趣的列。 當我沿着文件名軸連接時,我只得到感興趣的列和數字。 沒有文件名。
這是我按原樣使用的代碼。 它按我的預期工作,直到“all_filename”行。
for filename in os.listdir(directory):
if filename.endswith("log.csv"):
df = pd.read_fwf(filename, skiprows=186, nrows=1, names=["Attribute"])
df['System_Library_Name'] = [x.split('/')[6] for x in df['Attribute']]
df2= pd.concat([df for filename in os.listdir(directory)], keys=[filename])
df2.to_csv(filename+"log_info.csv", index=filename)
all_filenames = glob.glob(os.path.join(directory,'*log_info.csv'))
cat_log = pd.concat([pd.read_csv(f) for f in all_filenames ])
cat_log2= cat_log[['System_Library_Name']]
cat_log2.to_excel("log.xlsx", index=filename)
我嘗試將 keys=filename 添加到第三行到最后一行,並使用 df.index.name= 為索引命名
我之前使用過類似的代碼並且運行良好,但是如果這有所不同,這只是我從更大的原始輸入文件中使用的一列。
任何意見是極大的贊賞!
df = pd.concat(
# this is just reading one value from each file, yes?
[pd.read_fwf(filename, skiprows=186, nrows=1, names=["Attribute"])
.set_index(pd.Index([filename]))
.applymap(lambda x: x.split('/')[6])
.rename(columns={'Attribute':'System_Library_Name'})
for filename in glob.glob(os.path.join(directory,'*log.csv'))
]
)
df.to_xlsx("log_info.xlsx")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.