python - 标签合并数据框
问题描述
我已经将两个 DataFrame 合二为一,但不知道如何标记“state_x”和“state_y”tp“西海岸和“东海岸”。我稍后会绘制它们。
到目前为止我所拥有的:
West_quakes = pd.DataFrame({'state': ['California', 'Oregon', 'Washington', 'Alaska'],
'Occurrences': [18108, 376, 973, 12326]})
East_quakes = pd.DataFrame({'state': ['Maine', 'New Hampshire', 'Massachusetts',
'Connecticut', 'New York', 'New Jersey', 'Pennsylvania', 'Maryland',
'Virginia', 'North Carolina', 'South Carolina', 'Georgia', 'Florida'],
'Occurrences': [36, 13, 10, 5, 35, 10, 14, 2, 28, 17, 32, 14, 1]})
West_quakes.reset_index(drop=True).merge(East_quakes.reset_index(drop=True), left_index=True, right_index=True)
输出:
state_x Occurrences_x state_y Occurrences_y
0 California 18108 Maine 36
1 Oregon 376 New Hampshire 13
2 Washington 973 Massachusetts 10
3 Alaska 12326 Connecticut 5
我尝试过的其他合并方法但会导致语法错误,例如:
West_quake.set_index('West Coast', inplace=True)
East_quake.set_index('East Coast', inplace=True)
在谷歌上搜索并在这里搜索后,我真的迷路了。
任何帮助将不胜感激。
谢谢你。
解决方案
也许您正在寻找concat
:
pd.concat((West_quakes, East_quakes))
给出:
state Occurrences
0 California 18108
1 Oregon 376
2 Washington 973
3 Alaska 12326
0 Maine 36
1 New Hampshire 13
2 Massachusetts 10
3 Connecticut 5
4 New York 35
5 New Jersey 10
6 Pennsylvania 14
7 Maryland 2
8 Virginia 28
9 North Carolina 17
10 South Carolina 32
11 Georgia 14
12 Florida 1
或者:
pd.concat((West_quakes, East_quakes), keys=('West','East'))
这使:
state Occurrences
West 0 California 18108
1 Oregon 376
2 Washington 973
3 Alaska 12326
East 0 Maine 36
1 New Hampshire 13
2 Massachusetts 10
3 Connecticut 5
4 New York 35
5 New Jersey 10
6 Pennsylvania 14
7 Maryland 2
8 Virginia 28
9 North Carolina 17
10 South Carolina 32
11 Georgia 14
12 Florida 1
或者:
pd.concat((West_quakes, East_quakes), axis=1, keys=('West','East'))
输出:
West East
state Occurrences state Occurrences
0 California 18108.0 Maine 36
1 Oregon 376.0 New Hampshire 13
2 Washington 973.0 Massachusetts 10
3 Alaska 12326.0 Connecticut 5
4 NaN NaN New York 35
5 NaN NaN New Jersey 10
6 NaN NaN Pennsylvania 14
7 NaN NaN Maryland 2
8 NaN NaN Virginia 28
9 NaN NaN North Carolina 17
10 NaN NaN South Carolina 32
11 NaN NaN Georgia 14
12 NaN NaN Florida 1
推荐阅读
- php - Composer 诊断:Curl 错误 60 问题,代理问题?
- python - 是否可以从另一个扩展添加 vscode-python linter?
- python - 获取并压缩 txt 文件,然后将内容压缩到最大长度值为 50
- javascript - 如何使用数组 JavaScript 访问对象上的嵌套键?
- assembly - x86-64 在线汇编,带有 IDE,例如 https://www.mycompiler.io/new/asm-x86_64
- xcode - NSLocalizedDescription=模拟器不支持远程通知
- c++ - c++模板方法定义不能识别使用别名
- python - Difference between torch.flatten() and nn.Flatten()
- c - 无法在 C 中打印出带有可变参数的 char *
- perl - 如何在文件夹和子文件夹中运行命令