gpt4 book ai didi

python - 如何动态更改数据框名称

转载 作者:太空宇宙 更新时间:2023-11-04 09:58:45 25 4
gpt4 key购买 nike

我在 pandas 中命名数据框时遇到问题。有什么方法可以自动更改命名和索引,而不是手动逐行编写?

xl=pd.ExcelFile('Movement of BSE RIK.xlsx',header=0,dtype={'Open Price 

(Rs.)':np.float64})

p=xl.sheet_names

df= xl.parse('D-0')

df1= xl.parse('D-1')

df2= xl.parse('D-2')

df3= xl.parse('D-3')

df4= xl.parse('D-4')

df5= xl.parse('D-5')

df6= xl.parse('D-6')

df7= xl.parse('D-7')

df8= xl.parse('D-8')


df9= xl.parse('D-9')

df10= xl.parse('D-10')

df11= xl.parse('D-11')

df12= xl.parse('D-12')

df13= xl.parse('D-13')

df14= xl.parse('D-14')

df.set_index('Security Name',inplace=True)
df1.set_index('Security Name',inplace=True)
df2.set_index('Security Name',inplace=True)
df3.set_index('Security Name',inplace=True)
df4.set_index('Security Name',inplace=True)
df5.set_index('Security Name',inplace=True)
df6.set_index('Security Name',inplace=True)
df7.set_index('Security Name',inplace=True)
df8.set_index('Security Name',inplace=True)
df9.set_index('Security Name',inplace=True)
df10.set_index('Security Name',inplace=True)
df11.set_index('Security Name',inplace=True)
df12.set_index('Security Name',inplace=True)
df13.set_index('Security Name',inplace=True)
df14.set_index('Security Name',inplace=True)

此代码非常冗长,我必须进一步使用这些数据框名称。简而言之,我想尽量减少行数

最佳答案

我想你可以使用参数 sheetname=None用于创建 DataFramesdict:

dfs = pd.read_excel('Movement of BSE RIK.xlsx',
header=0,
dtype={'Open Price (Rs.)':np.float64},
sheetname=None,
index_col='Security Name')

然后您可以通过以下方式选择DataFrames:

print (dfs['D-0'])

print (type(dfs['D-0']))
<class 'pandas.core.frame.DataFrame'>

示例 file :

dfs = pd.read_excel('Movement of BSE RIK.xlsx',
header=0,
dtype={'Open Price (Rs.)':np.float64},
sheetname=None,
index_col='Security Name')
print (dfs)

print (dfs['D-0'])

a b Open Price (Rs.)
Security Name
1 r u 3.0
4 t j 5.0

关于python - 如何动态更改数据框名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44734499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com