想象一下,我有一个字典,里面有8个数据帧(名称不同) 每个数据框对应一个特定年份。例如,有2015年,2016年,2017年的患者就诊。每个数据框的行数(患者)不同,但列数相同(例如,他们进行的测试),所以我的命题如下表格
Years Type Size Value
2015 DataFrame (4,99) (here it has the column names)
2016 DataFrame (125,99)
2017 DataFrame (85,99)
2018 DataFrame (250,99)
and so on
如何访问这些数据帧的大小?因为我不能使用df.size例如,因为数据帧的名称不同。
# I create a list to save the number of entries per data frame
years = []
#j is a number for which after that number I want to calculate how many elements each data frame has
for i in range(j,number_of_years):
p1 = df.size
years.append(pl)
但是正如我所说,df.size无效。
编辑:首先,我阅读了一个包含多张纸的Excel文件。我用
#k is a string variable with the name of the excel file
file = pd.read_excel(k, sheet_name=None)
#Then I create the data frame from the dict. Basically it is a big data frame that combines all the data from my dict
df = pd.concat(file[frame] for frame in file.keys()).reset_index(drop=True)
因此每个数据框都是不同的年份(2015、2016、2017等),我想创建一个名为year的新列,该列写该行从哪一年开始。因为每个数据帧具有不同数量的行,所以我想到了前面提到的过程。我的最终结果应该是这样的
Years
2015
2015
2015
...
2016
2016
...
etc
到目前为止,我已经将我的词典从8个数据帧转换为1个包含所有患者的大数据帧。例如,前200行对应于2015年的患者。接下来的330行来自2016年。接下来的100行来自2017年,依此类推。我想在该大数据框中添加一列,以写出患者来自哪一年。我无法手动完成(从某种意义上说,第250行是2015年,依此类推),因为如果我阅读另一个excel文件,那么患者的数量就会有所不同
答案 0 :(得分:0)
您正在寻找类似的东西
years[len(years)-5:]
要访问最后5个数据帧大小,您只需编写{{1}}
答案 1 :(得分:0)
谢谢大家的帮助!事实证明,正如meowulf建议在年底将其更改为++ [len(file [df] .index)]