无法正确旋转来自 .csv 文件的数据框

发布于 2025-01-14 02:38:21 字数 3338 浏览 1 评论 0原文

我的目标是为了方便起见重新排序数据框。当我“手动”设置数据框时,我可以毫无问题地完成它。但是当我在读取 .csv 文件后尝试时,它失败了。我将用示例更好地解释:

“手动”数据框:

  • 我导入pandas
  • 然后我声明我将使用的数据框。
  • 我按照自己的意愿重新排列了表格。
  • 最后我看到了结果。

这是代码:

import pandas as pd

df = pd.DataFrame({'Mes': ['Enero', 'Enero', 'Enero', 'Febrero', 'Febrero', 'Febrero'],
                   'Anio': ['2019', '2020', '2021', '2019', '2020', '2021'],
                   'Ventas': [2, 3, 4, 5, 6, 7],})
print(df)

df_ventas = df.pivot(index='Mes', columns='Anio', values='Ventas')
print(df_ventas)

结果正是我想要的,我的表格从这个:

       Mes   Anio  Ventas
0    Enero  2019       2
1    Enero  2020       3
2    Enero  2021       4
3  Febrero  2019       5
4  Febrero  2020       6
5  Febrero  2021       7

到这个:

Anio      2019  2020  2021
Mes
Enero       2     3     4
Febrero     5     6     7

.csv文件:

import pandas as pd

data = pd.read_csv("archivo.csv")

df_ventas = data.pivot(index = "Mes" , columns = "Anio", values = "Ventas")
print(df_ventas)

.csv 文件具有相同的信息,但结果如下:

Traceback (most recent call last):   
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
        return self._engine.get_loc(casted_key)   
    File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc   
    File "pandas\_libs\index.pyx", line 108, in
        pandas._libs.index.IndexEngine.get_loc   
    File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in
        pandas._libs.hashtable.PyObjectHashTable.get_item   
    File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in
        pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Anio'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   
    File "D:\Usuarios\wrodriguez\Downloads\Codigos_Prueba_Python_RobotFramework\02_Vulgara\06_Graficas\01.py", line 10, in <module>
        df_ventas = data.pivot(index = "Mes" , columns = "Anio", values = "Ventas")   
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 7793, in pivot
        return pivot(self, index=index, columns=columns, values=values)   
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\pivot.py", line 505, in pivot
        data_columns = [data[col] for col in columns_listlike]
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\pivot.py", line 505, in <listcomp>
        data_columns = [data[col] for col in columns_listlike]    
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
        indexer = self.columns.get_loc(key)    
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
        raise KeyError(key) from err KeyError: 'Anio'

我一直在研究,但找不到任何有助于理解为什么无法像我手动那样读取它的内容。

My goal is to reorder a dataframe for my convenience. When I set the dataframe "manually" I can do it without problems. But when I try after reading a .csv file, it fails. I will explain better with the examples:

"Manual" Dataframe:

  • I import pandas
  • Then I declare the dataframe I will be working with.
  • I reorder the table as I wish it to be.
  • Finally I see the results.

And here is the code:

import pandas as pd

df = pd.DataFrame({'Mes': ['Enero', 'Enero', 'Enero', 'Febrero', 'Febrero', 'Febrero'],
                   'Anio': ['2019', '2020', '2021', '2019', '2020', '2021'],
                   'Ventas': [2, 3, 4, 5, 6, 7],})
print(df)

df_ventas = df.pivot(index='Mes', columns='Anio', values='Ventas')
print(df_ventas)

The result is exactly what I want, I table that went from this:

       Mes   Anio  Ventas
0    Enero  2019       2
1    Enero  2020       3
2    Enero  2021       4
3  Febrero  2019       5
4  Febrero  2020       6
5  Febrero  2021       7

To this:

Anio      2019  2020  2021
Mes
Enero       2     3     4
Febrero     5     6     7

.csv file:

import pandas as pd

data = pd.read_csv("archivo.csv")

df_ventas = data.pivot(index = "Mes" , columns = "Anio", values = "Ventas")
print(df_ventas)

The .csv file has the same info, but the result is the following:

Traceback (most recent call last):   
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
        return self._engine.get_loc(casted_key)   
    File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc   
    File "pandas\_libs\index.pyx", line 108, in
        pandas._libs.index.IndexEngine.get_loc   
    File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in
        pandas._libs.hashtable.PyObjectHashTable.get_item   
    File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in
        pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Anio'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   
    File "D:\Usuarios\wrodriguez\Downloads\Codigos_Prueba_Python_RobotFramework\02_Vulgara\06_Graficas\01.py", line 10, in <module>
        df_ventas = data.pivot(index = "Mes" , columns = "Anio", values = "Ventas")   
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 7793, in pivot
        return pivot(self, index=index, columns=columns, values=values)   
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\pivot.py", line 505, in pivot
        data_columns = [data[col] for col in columns_listlike]
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\pivot.py", line 505, in <listcomp>
        data_columns = [data[col] for col in columns_listlike]    
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
        indexer = self.columns.get_loc(key)    
    File "D:\Usuarios\wrodriguez\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
        raise KeyError(key) from err KeyError: 'Anio'

I have been researching but I can't find anything that helps to understand why it cant be read as I did manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

流年里的时光 2025-01-21 02:38:21

您使用的是 Anio 而不是 Año。更正一下:

df_ventas = data.pivot(index = "Mes" , columns = "Año", values = "Ventas")

You're using Anio instead of Año. Correct that:

df_ventas = data.pivot(index = "Mes" , columns = "Año", values = "Ventas")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文