how to merge three columns of data into one dataframe
时间: 2024-04-13 13:31:03 浏览: 147
DataFrame 数据合并实现(merge,join,concat)
To merge three columns of data into one DataFrame, you can use the `pd.concat()` function from the pandas library. Here's an example:
```python
import pandas as pd
# Example data
data1 = [1, 2, 3, 4, 5]
data2 = [6, 7, 8, 9, 10]
data3 = [11, 12, 13, 14, 15]
# Create separate Series for each column
column1 = pd.Series(data1, name='Column1')
column2 = pd.Series(data2, name='Column2')
column3 = pd.Series(data3, name='Column3')
# Concatenate the Series into a single DataFrame
df = pd.concat([column1, column2, column3], axis=1)
# Print the resulting DataFrame
print(df)
```
In the code above, we start by importing the pandas library. Then, we create three separate lists (`data1`, `data2`, `data3`) representing the data for each column.
Next, we create individual Series objects for each column using the `pd.Series()` function. We provide the data for each column and specify the desired column name using the `name` parameter.
Finally, we use the `pd.concat()` function to concatenate the Series objects along the columns axis (`axis=1`). This creates a single DataFrame (`df`) with three columns. We then print the resulting DataFrame.
The output will be:
```
Column1 Column2 Column3
0 1 6 11
1 2 7 12
2 3 8 13
3 4 9 14
4 5 10 15
```
This creates a DataFrame with three columns named "Column1", "Column2", and "Column3" containing the data from the original three columns.
阅读全文