我试图用pandas中的另一个列表值替换另一个列表值,但它引发了一个错误:cannot set using a multi-index selection indexer with a different length than the value。在pandas中使用列表值是错误的做法吗?谢谢
import pandas as pd
cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
'Price': [[123, 123123],[123, 123123],[123, 123123],[123, 123123]]
}
df = pd.DataFrame(cars, columns = ['Brand', 'Price'])
df[1, 'Price'] = [2342, 23423]
print(df)发布于 2020-09-25 17:22:43
使用DataFrame.loc
df.loc[1, 'Price'] = [2342, 23423]
print(df)
0 Honda Civic [123, 123123]
1 Toyota Corolla [2342, 23423]
2 Ford Focus [123, 123123]
3 Audi A4 [123, 123123]在pandas中使用列表值是错误的做法吗?
我认为在pandas中使用list不是good idea,如果可能的话,在这里创建2列。
发布于 2020-09-25 18:28:51
您最好在价格列中创建两个列,如this。但是,如果您仍然有充分的理由在Price列中有一个列表,并且想要更新它,请使用以下代码:
df.iloc[1, 1] = [2342, 23423]这是用于选择的基于整数位置的索引。“价格”在index=1位置列wise.So中,第二个1表示“价格”列,第一个1表示实际的行索引。
https://stackoverflow.com/questions/64061305
复制相似问题