Python-Pandas所遇问题汇总

一、读取excel 或者 CSV时会多出一列index

解决方案:

#方法一:导出数据时index设为False
df3.to_excel('test.xlsx',index=False)
df = pd.read_excel('test.xlsx')
print(df)
'''
执行结果:
        用户名      密码
4        vv   45678
5        vv   45678
6        vv   45678
7        vv   45678
8        vv   45678
9        vv   45678
10       vv   45678
11       vv   45678
12       vv   45678
'''

#方法二:导入数据时pd.read_excel(filename,index_col=0)中index_col设置为0
df = pd.read_excel('test.xlsx',index_col=0)
print(df)

 二、DataFame对象转换为字典

解决方案:

dd = df.to_dict(orient='list')   #嵌套字典设为list类型
print(dd)
'''
执行结果:
{'用户名': ['小林', '小方', '小辉', '小天', 'vv', 'vv', 'vv', 'vv', 'vv', 'vv', 'vv', 'vv', 'vv', 'vv'], '密码': [123456, 123456, 123456, 123456, 45678, 45678, 45678, 45678, 45678, 45678, 45678, 45678, 45678, 45678]}
'''
三十六般武艺,七十二般变化,修练出个人品牌并发出光芒
原文地址:https://www.cnblogs.com/deeptester-vv/p/15123885.html