pandas.concatenate连接excel中两列
时间: 2024-04-30 11:21:06 浏览: 21
假设你有一个Excel文件,其中Sheet1中包含两列数据,分别为A列和B列。现在你想将这两列数据连接起来,可以使用 pandas 库的 concat() 方法。
示例代码如下:
```
import pandas as pd
# 读取Excel文件
df = pd.read_excel('your_file.xlsx', sheet_name='Sheet1')
# 将A列和B列连接起来,并保存到C列中
df['C'] = pd.concat([df['A'], df['B']])
# 输出结果
print(df)
```
在上面的代码中,我们首先使用 pd.read_excel() 方法读取了Excel文件,并将其存储在变量 df 中。然后,我们使用 pd.concat() 方法将 A 列和 B 列连接起来,并将结果存储在 C 列中。最后,我们使用 print() 方法输出结果。
相关问题
pd.read_excel指定两列读取
Sure, I can help you with that! To read only specific columns from an Excel file using pd.read_excel, you need to use the parameter "usecols" and pass in a list of the column names or indices that you want to read. For example, if your Excel file has columns named "Name", "Age", "Gender", and "Salary", and you only want to read the "Name" and "Salary" columns, you can use the following code:
```
import pandas as pd
df = pd.read_excel('your_file.xlsx', usecols=['Name', 'Salary'])
```
Hope this helps! Let me know if you have any more questions.
生成一个pandas.core.series.Series有两列
可以使用pandas库中的DataFrame构造函数来创建一个有两列的Series对象,然后再将其中一列转换为Series对象即可。以下是示例代码:
```python
import pandas as pd
# 创建一个DataFrame对象
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']})
# 将其中一列转换为Series对象
series = df['col1']
print(series)
```
输出结果:
```
0 1
1 2
2 3
Name: col1, dtype: int64
```