相关疑难解决方法(0)

设置可迭代项时必须具有相等的len键和值

我有两个数据框作为流:

leader:
    0 11
    1 8
    2 5
    3 9
    4 8
    5 6
    [6065 rows x 2 columns]

DatasetLabel:    
    Unnamed: 0      0    1  ....    7     8    9  10  11  12  
    0               A    J  ....    1     2    5 NaN NaN NaN  
    1               B    K  ....    3     4   NaN  NaN NaN NaN  

    [4095 rows x 14 columns]
Run Code Online (Sandbox Code Playgroud)

信息数据集的列名称0到6是关于数据的DatasetLabel,而7到12是引用领导者Dataframe的第一列的索引。

我想创建一个数据集,而不是DatasetLabel Dataset中的索引,而是从leader数据集中获取每个索引的值,即 leader.iloc[index,1]

我该如何使用python功能?

输出应如下所示:

 DatasetLabel:    
        Unnamed: 0      0    1  ....    7     8    9  10  11  12  
        0               A    J  ....    8     5 …
Run Code Online (Sandbox Code Playgroud)

python pandas

2
推荐指数
2
解决办法
1万
查看次数

ValueError:使用可迭代设置时必须具有相等的 len 键和值

当我运行这个玩具代码时

test = pd.DataFrame({'a': [1, 2, 3, 4]})
test['b'] = ''
for i in range(len(test)):
    test['b'].loc[i] = [5, 6, 7]
Run Code Online (Sandbox Code Playgroud)

我有一个警告

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_single_block(indexer, value, name)
Run Code Online (Sandbox Code Playgroud)

loc但如果我按照这种方法使用

test = pd.DataFrame({'a': [1, 2, 3, 4]})
test['b'] = ''
for i in range(len(test)):
    test.loc[i, 'b'] = [5, 6, 7]
Run Code Online (Sandbox Code Playgroud)

我收到一个错误

ValueError: Must have equal len keys and …
Run Code Online (Sandbox Code Playgroud)

python dataframe python-3.x pandas

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

pandas ×2

python ×2

dataframe ×1

python-3.x ×1