我如何将CSV的名称设置为DataFrame中的列
我有300个时间序列的CSV,其中不包含其中包含的ID,该文件中包含的名为All_DataFrames
,我正在尝试将CSV的名称添加为列的名称为“名称”
,例如
df1.csv
date value
2020-01-01 35
2020-01-02 40
2020-01-03 45
df2.csv
date value
2022-03-01 15
2022-03-02 25
2022-03-03 20
预期的输出将为:
df1.csv
name date value
df1 2020-01-01 35
df1 2020-01-02 40
df1 2020-01-03 45
df2.csv
name date value
df2 2022-03-01 15
df2 2022-03-02 25
df2 2022-03-03 20
我尝试使用以下代码作为做到这一点的方法,但是它不断向我投掷错误emptarydataeRor:neverydataeRor:note
emptiondataeRor:没有列从文件
即使我处于正确的工作目录中。
import pandas as pd
import glob
import os.path
# Create a list of all CSV files in folder
files = glob.glob("*.csv")
# Creats list of filenames for appending to df
filenames = []
# Doing the thing
for csv in files:
df = pd.read_csv(csv)
df['name'] = os.path.basename(csv)
filenames.append(df)
是否有一种更简单的方法来执行此操作,或者我应该尝试解决工作目录错误?
我发现一个与此非常相似的推荐问题,但是,它在R中,这不是我在这一点上都感到满意的语言。 r:in列表中的data from list in List in List in List in List in List数据范围
I have 300 time series CSV's that do not contain IDs within them contained in a file called all_dataframes
, and I am trying to go about adding a the name of the CSV as a column 'name'
For example,
df1.csv
date value
2020-01-01 35
2020-01-02 40
2020-01-03 45
df2.csv
date value
2022-03-01 15
2022-03-02 25
2022-03-03 20
The expected output would be:
df1.csv
name date value
df1 2020-01-01 35
df1 2020-01-02 40
df1 2020-01-03 45
df2.csv
name date value
df2 2022-03-01 15
df2 2022-03-02 25
df2 2022-03-03 20
I have tried using the below code as a way to do it, but it keeps throwing errors at me of EmptyDataError: No columns to parse from file
even though I am in the correct working directory.
import pandas as pd
import glob
import os.path
# Create a list of all CSV files in folder
files = glob.glob("*.csv")
# Creats list of filenames for appending to df
filenames = []
# Doing the thing
for csv in files:
df = pd.read_csv(csv)
df['name'] = os.path.basename(csv)
filenames.append(df)
Is there a more simple way to do this or should I try to fix the working directory error?
I found a recommended question very similar to this, however, it is in R and that's not a language I am comfortable with at this point.
R: Set column name from dataframe name in list of dataframes
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能有一个空文件?从文档中:
当遇到空数据或标头时, /代码>:
输出:
You have probably an empty file? From the documentation:
Tip: replace
glob
bypathlib
:Output:
我能够通过丢下错误并跳过损坏的文件来使它起作用:
I was able to get this to work by throwing an error and skipping over the corrupt file with: