Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用另一个pandas.DataFrame中的数据填充pandas.DataFrame中的列?_Python_Pandas - Fatal编程技术网

Python 如何使用另一个pandas.DataFrame中的数据填充pandas.DataFrame中的列?

Python 如何使用另一个pandas.DataFrame中的数据填充pandas.DataFrame中的列?,python,pandas,Python,Pandas,我有第一个pandas.DataFrame first_key second_key 0 0 1 1 0 1 2 0 2 3 0 3 4 0 3 key status 0 1

我有第一个pandas.DataFrame

        first_key  second_key
0               0           1
1               0           1
2               0           2
3               0           3
4               0           3
              key      status
0               1       'good'
1               2        'bad'
2               3       'good'
        first_key  second_key  status
0               0           1  'good'
1               0           1  'good'
2               0           2   'bad'
3               0           3  'good'
4               0           3  'good'
还有第二个pandas.DataFrame

        first_key  second_key
0               0           1
1               0           1
2               0           2
3               0           3
4               0           3
              key      status
0               1       'good'
1               2        'bad'
2               3       'good'
        first_key  second_key  status
0               0           1  'good'
1               0           1  'good'
2               0           2   'bad'
3               0           3  'good'
4               0           3  'good'
我想得到下面的pandas.DataFrame

        first_key  second_key
0               0           1
1               0           1
2               0           2
3               0           3
4               0           3
              key      status
0               1       'good'
1               2        'bad'
2               3       'good'
        first_key  second_key  status
0               0           1  'good'
1               0           1  'good'
2               0           2   'bad'
3               0           3  'good'
4               0           3  'good'
如何做到这一点

从第二个数据帧创建的系列使用:

df['status'] = df['second_key'].map(df1.set_index('key')['status'])
print (df)
   first_key  second_key  status
0          0           1  'good'
1          0           1  'good'
2          0           2   'bad'
3          0           3  'good'
4          0           3  'good'
由第二个数据帧创建的系列使用:

df['status'] = df['second_key'].map(df1.set_index('key')['status'])
print (df)
   first_key  second_key  status
0          0           1  'good'
1          0           1  'good'
2          0           2   'bad'
3          0           3  'good'
4          0           3  'good'

您还可以使用
merge()
方法:

In [75]: d1.merge(d2.rename(columns={'key':'second_key'}))
Out[75]:
   first_key  second_key  status
0          0           1  'good'
1          0           1  'good'
2          0           2   'bad'
3          0           3  'good'
4          0           3  'good'

如果您想添加单个列,则@jezrael的答案中显示的
.map()
方法更可取(也更有效)。如果需要添加多列,请使用
.merge()
方法。

也可以使用
merge()
方法:

In [75]: d1.merge(d2.rename(columns={'key':'second_key'}))
Out[75]:
   first_key  second_key  status
0          0           1  'good'
1          0           1  'good'
2          0           2   'bad'
3          0           3  'good'
4          0           3  'good'
如果您想添加单个列,则@jezrael的答案中显示的
.map()
方法更可取(也更有效)。如果需要添加多列,请使用
.merge()
方法。

pd.DataFrame.join
pd.DataFrame.join

下面是使用函数(而不是
merge
方法)的另一个示例


IMHO,使用该函数使转换稍微显式一些。但这显然是一个问题或味道…

这里是另一个使用函数的示例(而不是
merge
方法)

IMHO,使用该函数使转换稍微显式一些。但这显然是一个问题或味道