既存の数値列、列名などの文字列のリストと値としてタプルのリストに基づいてデータフレームに新しい列を作成します。

ジョン・ウィーズナー:

私は、数値列を含むデータフレームを持っていると私はタプルのリストや文字列のリストを持っています。タプルのリストは、そのリスト内の各インデックスは、データフレーム内の数値列に対応する追加されるべき値を表します。文字列のリストを追加する列の名前を表します。

例:

import pandas as pd

df = pd.DataFrame({'number':[0,0,1,1,2,2,3,3]})

# a list of keys and a list of tuples
keys = ['foo','bar']
combinations = [('99%',0.9),('99%',0.8),('1%',0.9),('1%',0.8)]

予想される出力:

   number  foo  bar
0       0  99%  0.9
1       0  99%  0.9
2       1  99%  0.8
3       1  99%  0.8
4       2   1%  0.9
5       2   1%  0.9
6       3   1%  0.8
7       3   1%  0.8
Josmoor98:

オリジナルポスト

その出力を取得するには、あなただけ試すことができます

df2 = pd.DataFrame(combinations, columns = keys)
pd.concat([df, df2], axis=1)

そのリターン

   number   foo   bar
0       0   99%   0.9
1       1   99%   0.8
2       2   1%    0.9
3       3   1%    0.8

編集します

あなたの新しい要件に基づいて、次を使用することができます

df.set_index('number', inplace=True)
df = df.merge(df2, left_index = True, right_index=True)
df = df.reset_index().rename(columns={'index':'number'})

また、これはすなわち、異なる重複量のために働きます

df = pd.DataFrame({'number':[0,0,1,1,1,2,2,3,3,3]})

戻り値

   number   foo   bar
0       0   99%   0.9
1       0   99%   0.9
2       1   99%   0.8
3       1   99%   0.8
4       1   99%   0.8
5       2   1%    0.9
6       2   1%    0.9
7       3   1%    0.8
8       3   1%    0.8
9       3   1%    0.8

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=16150&siteId=1