python的union all
时间: 2023-12-21 08:02:24 浏览: 103
UNION ALL 的使用方法
在Python中,可以使用pandas库来实现类似SQL中的union all操作。以下是两种方法可以实现这个功能:
方法一:
```
import pandas as pd
df1 = pd.DataFrame([{'col1':'a', 'col2':1}])
df2 = pd.DataFrame([{'col1':'b', 'col2':2}])
df3 = pd.DataFrame([{'col1':'c', 'col2':3}])
lists = [df1, df2, df3]
df = pd.concat(lists)
print(df)
```
输出结果:
```
col1 col2
0 a 1
0 b 2
0 c 3
```
方法二:
```
import pandas as pd
df1 = pd.DataFrame([{'col1':'a', 'col2':1}])
df2 = pd.DataFrame([{'col1':'b', 'col2':2}])
df = df1.append(df2)
print(df)
```
输出结果:
```
col1 col2
0 a 1
0 b 2
```
以上是使用pandas库来实现Python的union all操作的两种方法。
另外,在MySQL中也可以使用UNION ALL来实现类似的功能。以下是一个MySQL的示例:
```
SELECT name, country
FROM test_user
UNION ALL
SELECT name, country
FROM test_user_copy
ORDER BY country;
```
输出结果:
```
| name | country |
|--------|-----------|
| Swift | China |
| JAVA | China |
| SQL | China |
| JAVA | China |
| Php | China |
| Go | China |
| Python | Singapore |
| R | Singapore |
| Python | Singapore |
| C# | Singapore |
```
以上是在MySQL中使用UNION ALL来实现Python的union all操作的示例。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [python dataframe unionall](https://blog.csdn.net/chinacmt/article/details/52189483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [python:mysql之UNION操作](https://blog.csdn.net/LIFENG0402/article/details/118610123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文