社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Ralph

Ralph 最近回复了

正如@DanielLabbe所说,这很可能是matplotlib版本的问题。可以检查matplotlib的版本:

import matplotlib as mpl
mpl.__version__

如果您具有sudo/管理权限,则需要更新其中一台计算机上的matplotlib,以便它们都运行同一版本。如果无法执行此操作,请显式指定 align 论证 plt.hist 可能会有帮助。

plt.hist(my_data, align='right')
6 年前
回复了 Ralph 创建的主题 » 在python中从excel文件导入pandas数据框时发生类型错误

现在,有一个选项 read_excel 它允许我们改变 dtype 但是没有这样的选项来更改 D型 任何一排的。所以,在读入数据之后,我们必须自己进行类型转换。

正如你在问题中所展示的, df['a'][1] 有类型 str ,但你希望它有类型 list .

所以,假设我们有一些绳子 l ='[1, 2, 3]' 我们可以把它转换成一个int列表( l=[1, 2, 3] 作为 [int(val) for val in l.strip('[]').split(',')] . 现在,我们可以将它与 .apply 得到我们想要的东西的方法:

df.iloc[1] = df.iloc[1].apply(lambda x : [int(val) for val in x.strip('[]').split(',')])

把这个例子放在一起,我们有:

import pandas as pd

# Data as read in by read_excel method
df2 = pd.DataFrame({'a' : [3, '[1, 2, 3]', 'text1'],
                   'b' : [4, '[4, 5, 6, 7]', 'text2']})
print('Type: ', type(df2['a'][1]))
#Type:  <class 'str'>

# Convert strings in row 1 to lists
df2.iloc[1] = df2.iloc[1].apply(lambda x : [int(val) for val in x.strip('[]').split(',')])

print('Type: ', type(df2['a'][1]))
#Type:  <class 'list'>

dict2 = df2.to_dict(orient='list')
6 年前
回复了 Ralph 创建的主题 » python while循环永不结束(在numworks计算器上)

你需要比较 type 属于 text float . 要做到这一点,保持你原来的逻辑,一种方法是:

# controle de saisie d'un nombre
def inputFloat(text):
    ret = ''
    while type(ret) != float:
        try:
            ret = float(input(text + " (nombre)"))
        except ValueError:
            print("saisie incorrecte.")
    return ret

或者,您可以使用: while not isinstance(ret, float) ( isinstance 实际上是检查python中类型的首选方法)。

正如@iguananaut在评论中提到的,您可以通过删除 ret 变化无常。

def input_float():
    while True:
        try:
            return float(input("(nombre): "))
        except ValueError:
            print("saisie incorrecte.")

编辑

让这个和你的 test 功能和你的第二个 while 循环需要在以下情况下包含break子句 test() 返回:

# affichage du menu
while True:
  print("[1] test")
  print("[0] quitter")
  choix = input("Choisir une fonction:")
  if choix == "0":
    print("au revoir.")
    break
  elif choix == "1":
    test()
    break

注意,如果您选择使用我建议的第二个函数( input_float )您需要将测试函数更改为

# test
def test():
  print(input_float())