[英]Pandas dataframe set_index not accepting array
I have a simple function in python to programmatically load a csv, transpose a number of columns and export back as csv我在 python 中有一个简单的函数来以编程方式加载 csv,转置一些列并导出为 csv
## transpose columns ##
def stack_file(input, indexes, delimiter):
df = pd.read_csv(input, sep=delimiter)
print(df.columns.values)
print(indexes)
#df.set_index(['Province/State','Country/Region','Lat','Long'], inplace=True)
df.set_index(indexes, inplace=True)
df = df.stack()
df.to_csv(path.join(path.dirname(input),path.basename(input)),sep="\t")
Now you can see in the commented line the function called with a test array - using that line works.现在您可以在注释行中看到使用测试数组调用的函数 - 使用该行有效。 If I try to pass an array, I get the following error:如果我尝试传递数组,则会收到以下错误:
ValueError: Length mismatch: Expected 30870 rows, received array of length 1 ValueError:长度不匹配:预期为 30870 行,收到长度为 1 的数组
The array i'm passing is generated in the following way and if I print it, it displays exactly like the one in the comment line我传递的数组是按以下方式生成的,如果我打印它,它显示的与注释行中的完全一样
header_indexes = np.array([])
for x in range(0, header_index_last):
header_indexes = np.append(header_indexes, column[x])
I've tried to look at documentation but I really don't understand why this is not working...我试图查看文档,但我真的不明白为什么这不起作用......
The problem here is passing a numpy array to set_index.这里的问题是将一个 numpy 数组传递给 set_index。 Convert it to a list and it should work.将其转换为列表,它应该可以工作。
So replace所以更换
df.set_index(indexes, inplace=True)
with和
df.set_index(indexes.tolist(), inplace=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.