weixin_55048943
2022-04-24 10:52
采纳率: 100%
浏览 77

pandas dataframe数据的列如何转换为列表?

问题遇到的现象和发生背景

pandas dataframe数据的列如何转换为列表?

问题相关代码,请勿粘贴截图
import csv
import pandas as pd
import numpy as np
from pyecharts.charts import Bar, Page
from pyecharts import options as opts
import os
# df=pd.read_csv("C:/Users/EJIHZHA/Desktop/KB数据分析/PEV.csv",index_col=0,na_values=0)  #将0定义为额外缺失值
df=pd.read_csv("C:/Users/EJIHZHA/Desktop/KB数据分析/PEV.csv")                  #读取Csv数据以df命名

df1=df.pivot_table(
    index="creat_date",
    columns="MPA_area",
    aggfunc={"MPA_area":"count"},
    fill_value=0,
    # margins_name="总计"
    
    )

print(df1)

# print(mods_A_list)
#绘制图表:
c_date=df1.index.to_list()                             #可以转为list
mods_A_list=df1.loc[:,"Module-A"].tolist()     #无法转为list

print(mods_A_list)
运行结果及报错内容

Traceback (most recent call last):
File "C:\Users\EJIHZHA\Desktop\KB数据分析\KB_Anl.py", line 32, in
bar.add_yaxis("Module-A",df1["Module-A"].round(0).to_list(),itemstyle_opts=opts.ItemStyleOpts(color='blue'))
File "C:\Users\EJIHZHA\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3504, in getitem
return self._getitem_multilevel(key)
File "C:\Users\EJIHZHA\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3555, in _getitem_multilevel
loc = self.columns.get_loc(key)
File "C:\Users\EJIHZHA\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\multi.py", line 2871, in get_loc
loc = self._get_level_indexer(key, level=0)
File "C:\Users\EJIHZHA\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\multi.py", line 3224, in _get_level_indexer
idx = self._get_loc_single_level_index(level_index, key)
File "C:\Users\EJIHZHA\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\multi.py", line 2804, in _get_loc_single_level_index
return level_index.get_loc(key)
File "C:\Users\EJIHZHA\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc
raise KeyError(key) from err
KeyError: 'Module-A'

img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题