每次我尝试使用 Python 中的 Pandas 重新组织此数据集中的列时,所有值都会变成 Nan。我该如何解决?
Everytime I try to reorganize the columns in this data set using Pandas in Python, all values turn Nan. How do I fix this?
作为作业的一部分,我正在尝试以(咖啡)类型按字母顺序排列的方式重新组织列。我实现了以下代码,它确实有效:
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
BeanFileCleaned = BeanFileCleaned.reindex(columns=Reorganized)
BeanFileCleaned
但是,当我再次 运行 文件时, table 中的所有值都变成 Nan 并且由于某种原因,行也与列同名(有很多行,所以这不应该发生)。
我该如何解决这个问题?感谢任何帮助,提前致谢。
编辑:忘了补充,结果是这样的:
假设 BeanFileCleaned 是一个 pandas 数据框,您可能正在寻找这样的东西。
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
Reorganized = sorted(Reogranized)
BeanFileCleaned = BeanFileCleaned[Reorganized]
如果您有额外的列要追加到末尾,您可以这样做
all_columns = BeanFileCleaned.columns
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
Reorganized = sorted(Reogranized)
Reorganized_set = set(Reorganized)
rest_columns = [column for column in all_columns if column not in Reorganized_set]
BeanFileCleaned = BeanFileCleaned[Reorganized + rest_columns]
作为作业的一部分,我正在尝试以(咖啡)类型按字母顺序排列的方式重新组织列。我实现了以下代码,它确实有效:
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
BeanFileCleaned = BeanFileCleaned.reindex(columns=Reorganized)
BeanFileCleaned
但是,当我再次 运行 文件时, table 中的所有值都变成 Nan 并且由于某种原因,行也与列同名(有很多行,所以这不应该发生)。
我该如何解决这个问题?感谢任何帮助,提前致谢。
编辑:忘了补充,结果是这样的:
假设 BeanFileCleaned 是一个 pandas 数据框,您可能正在寻找这样的东西。
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
Reorganized = sorted(Reogranized)
BeanFileCleaned = BeanFileCleaned[Reorganized]
如果您有额外的列要追加到末尾,您可以这样做
all_columns = BeanFileCleaned.columns
#Rearrange the names of the Coffees so they are in alphabetical order
Reorganized = ["Channel", "Region", "Arabica", "Cappuccino", "Espresso", "Latte", "Lungo"]
Reorganized = sorted(Reogranized)
Reorganized_set = set(Reorganized)
rest_columns = [column for column in all_columns if column not in Reorganized_set]
BeanFileCleaned = BeanFileCleaned[Reorganized + rest_columns]