pandas loc[]赋值报错
ValueError: shape mismatch: value array of shape (2,) could not be broadcast to indexing result of shape (2,5)
df2.loc[data_index]=line_data
解决:改成:
line_data=matrix_data.iloc[i.get('data1')]
df2.loc[i.get('data1')]=line_data.tolist()
Error 2:
print(df.index.str.contains('a'))执行报错,但是执行df.columns.str.contains()没有报错
AttributeError: Can only use .str accessor with Index, not MultiIndex
agile_data1 = {
'school' : ['北京大学', "", '山西大学', '山西学', '武汉大学1'],
'name' : ['江路离', '沈希梦', '来使鹭', '陈曦冉', '姜浩然'],
'AP' : ['江路离', '沈希梦', '来使鹭', '陈曦冉', '姜浩然'],
'No.' : [20001943, 300044451, 20190006, 20191234, 1242522]
}
df = pd.DataFrame(agile_data1,index=[list('abcde')])
# 提取行名称满足条件的行
# 将字符串方法应用于可以通过index属性获得的索引对象。
print("--------\n",df)
print(df.index)
# Index(['Alice', 'Bob', 'Charlie', 'Dave', 'Ellen', 'Frank'], dtype='object', name='name')
print("search columns------------")
print(df.columns.str.endswith('e'))
print(df.columns.str.contains('e'))
print('search index')
print(df.index.str.contains('a'))
# [ True False True False False False]