import pandas as pd
videos_list = {
'Video ID': ['aaa', 'bbb', 'ccc'],
'Title': ['Video Title AAA', 'Video Title BBB', 'Video Title CCC'],
'Views': ['100', '30', '60']}
transcripts_list = {
'Title': ['Video Title AAA', 'Video Title CCC'],
'Video ID': ['aaa', 'ccc'],
'Rating': ['99', '33']}
videos = pd.DataFrame(videos_list)
transcripts = pd.DataFrame(transcripts_list)
## VIEW Videos and Transcript TABLES
print('--- VIDEOS:n',list(videos.columns.values),'n',videos.head(5),'n')
print('--- Transcripts:n',list(transcripts.columns.values),'n',transcripts.head(5),'n')
## Remove 'Title' from transcripts
transcript_cols = [
'Video ID',
'Rating',
]
transcript_reindex = transcripts.reindex(columns=transcript_cols)
print('--- Transcript Reindex:n',list(transcript_reindex.columns.values),'n',transcript_reindex.head(5),'n')
## Merge videos + transcript_reindex
transcript_video = pd.merge(videos, transcript_reindex, left_on='Video ID', right_on='Video ID')
print('Video + Transcript:n',list(transcript_video.columns.values),'n',transcript_video.head(5))
transcript_video.to_excel('Results.xlsx', index=False, na_rep='')
上述代码正确工作,产生以下结果:
视频ID视频标题aaa视频标题ccc这有帮助吗?
import os
import glob
import pandas as pd
os.chdir("C:\Users\ryans\OneDrive\Desktop\schemas\")
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
#combine all files in the list
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
#export to csv
combined_csv.to_csv( "C:\combined.csv", index=False, encoding='utf-8-sig')
或者,这个?
from glob import glob
with open('C:/main.csv', 'a') as singleFile:
for csv in glob('C:/Users/*.csv'):
if csv == 'main.csv':
pass
else:
for line in open(csv, 'r'):
singleFile.write(line)