简体   繁体   English

读取多个 CSV,每列都有其 CSV 名称

[英]Read multiple CSV an each column has its CSV name

to sum up i need that each colum as the name that the csv file has.总而言之,我需要将每个列作为 csv 文件的名称。

This is what I have done so far :这是我到目前为止所做的:

path = r'C:\Users\dfgdfsgsfg\Untitled Folder\tickers' # use your path
all_files = glob.glob(os.path.join(path, "*.csv"))     # advisable to use os.path.join as this makes concatenation OS independent

df_from_each_file = (pd.read_csv(f , parse_dates=True, index_col="date") for f in all_files)

concat = pd.concat(df_from_each_file, axis=1)

df = concat['PriceUSD']

df.columns = [ ??????? ] #what do I put in here?

This what I get when I dont name the columns这是我不命名列时得到的结果

i also tryied this , but not quiet the results i wished我也试过这个,但没有安静我希望的结果

all_files = glob.glob(os.path.join(path, "*.csv"))     # advisable to use os.path.join as this makes concatenation OS independent

df_from_each_file = (pd.read_csv(f , parse_dates=True, index_col="date").assign(filename = f) for f in all_files)

concat = pd.concat(df_from_each_file, axis=1)

df = concat['PriceUSD']

df.columns = all_files[:-2]

df

RESULT结果

If you are really interested in only the single column from all those CSV files, then while parsing the csv just trim it to just the column you want:如果您真的只对所有这些 CSV 文件中的单列感兴趣,那么在解析 csv 时只需将其修剪为您想要的列:

def getPriceUSD(filename):
    """reads csv file then returns dataframe with just the column 'PriceUSD'
    with the filename as the column title"""
    data = pd.read_csv(f , parse_dates=True, index_col="date")
    data = data["PriceUSD"]
    data.columns = [filename]
    return data

Then concat all the already parsed and formatted columns together:然后将所有已经解析和格式化的列连接在一起:

df = pd.concat(map(getPriceUSD, all_files), axis=1)

And before you ask, if you don't want the full path then use os.path.basename(filename) for the column instead of just filename在你问之前,如果你不想要完整路径,那么对列使用os.path.basename(filename)而不是filename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM