我如何将CSV的名称设置为DataFrame中的列

发布于 2025-02-02 05:42:26 字数 1445 浏览 3 评论 0原文

我有300个时间序列的CSV,其中不包含其中包含的ID,该文件中包含的名为All_DataFrames,我正在尝试将CS​​V的名称添加为列的名称为“名称”

,例如

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2025-02-09 05:42:26

您可能有一个空文件?从文档中:

在遇到空数据或标头时,在pd.read_csv(c和python发动机)中抛出的例外

当遇到空数据或标头时, /代码>:

import pandas as pd
import pathlib

for csvfile in pathlib.Path('.').glob('*.csv'):
    df = pd.read_csv(csvfile)
    df.insert(0, 'name', csvfile.stem)
    df.to_csv(csvfile, index=False)

输出:

>>> %cat df1.csv
name,date,value
df1,2020-01-01,35
df1,2020-01-02,40
df1,2020-01-03,45

>>> %cat df2.csv
name,date,value
df2,2022-03-01,15
df2,2022-03-02,25
df2,2022-03-03,20

You have probably an empty file? From the documentation:

pandas.errors.EmptyDataError

Exception that is thrown in pd.read_csv (by both the C and Python engines) when empty data or header is encountered

Tip: replace glob by pathlib:

import pandas as pd
import pathlib

for csvfile in pathlib.Path('.').glob('*.csv'):
    df = pd.read_csv(csvfile)
    df.insert(0, 'name', csvfile.stem)
    df.to_csv(csvfile, index=False)

Output:

>>> %cat df1.csv
name,date,value
df1,2020-01-01,35
df1,2020-01-02,40
df1,2020-01-03,45

>>> %cat df2.csv
name,date,value
df2,2022-03-01,15
df2,2022-03-02,25
df2,2022-03-03,20
原来分手还会想你 2025-02-09 05:42:26

我能够通过丢下错误并跳过损坏的文件来使它起作用:

for csv in files:
    try:
        print(csv)
        df = pd.read_csv(csv)
        df['name'] = os.path.basename(csv)
        filenames.append(df)
    except:
        print("Error with" + csv)

I was able to get this to work by throwing an error and skipping over the corrupt file with:

for csv in files:
    try:
        print(csv)
        df = pd.read_csv(csv)
        df['name'] = os.path.basename(csv)
        filenames.append(df)
    except:
        print("Error with" + csv)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文