dataframe怎么把行拼接起来
时间: 2023-06-17 15:08:21 浏览: 48
可以使用`concat()`函数将多个DataFrame按行拼接起来。
比如有两个DataFrame df1 和 df2,可以使用以下代码将它们按行拼接起来:
```python
import pandas as pd
# 创建两个DataFrame
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
# 使用concat函数按行拼接
result = pd.concat([df1, df2], axis=0)
print(result)
```
输出结果:
```
A B
0 1 3
1 2 4
0 5 7
1 6 8
```
其中,`axis=0`表示按行拼接,如果`axis=1`则表示按列拼接。如果要重置行索引,可以在拼接后使用`reset_index()`函数。
相关问题
dataframe按行拼接
可以使用 pandas 库中的 concat 函数来实现 dataframe 按行拼接。具体代码如下:
```python
import pandas as pd
# 创建两个 dataframe
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
# 按行拼接两个 dataframe
result = pd.concat([df1, df2], axis=0)
print(result)
```
输出结果为:
```
A B
0 1 3
1 2 4
0 5 7
1 6 8
```
其中,concat 函数的第一个参数是要拼接的 dataframe 列表,第二个参数 axis=0 表示按行拼接。
pandas将dataframe按行拼接
以下是两种pandas将DataFrame按行拼接的方法:
1. 使用.append()方法
```python
import pandas as pd
df1 = pd.DataFrame([[1, 2], [3, 4]])
df2 = pd.DataFrame([[5, 6], [7, 8]])
df = df1.append(df2)
print("df1的值为:")
print(df1)
print("df2的值为:")
print(df2)
print("df的值为:")
print(df)
```
2. 使用.concat()方法
```python
import pandas as pd
df1 = pd.DataFrame([[1, 2], [3, 4]])
df2 = pd.DataFrame([[5, 6], [7, 8]])
df = pd.concat([df1, df2], ignore_index=True)
print("df1的值为:")
print(df1)
print("df2的值为:")
print(df2)
print("df的值为:")
print(df)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)