pandas 用institution表中的ts_code和private_investor表中的ts_code求交集,并且合并表
时间: 2024-05-05 21:22:19 浏览: 22
假设institution表中包含列为'ts_code'和'inst_name',private_investor表中包含列为'ts_code'和'investor_name',可以使用pandas中的merge函数实现交集和合并表。
代码示例:
```python
import pandas as pd
# 读取institution表和private_investor表
institution = pd.read_csv('institution.csv')
private_investor = pd.read_csv('private_investor.csv')
# 使用merge函数合并表,并且求交集
result = pd.merge(institution, private_investor, on='ts_code', how='inner')
# 输出合并后的结果
print(result)
```
其中,参数on指定合并的列为'ts_code',参数how指定合并方式为'inner',即求交集。最后输出合并后的结果。
相关问题
python df中ts_code列值开头为864和866的不保存
如果您想要实现这个功能,您可以使用 Pandas 库来读取和处理表格数据。
首先,您可以使用 Pandas 的 read_excel 函数读取 Excel 文件,例如:
```python
import pandas as pd
df = pd.read_excel('file.xlsx')
```
然后,您可以使用 Pandas 的 loc 函数和字符串方法 str.startswith 来选取 ts_code 列开头不为 864 或 866 的行,例如:
```python
filtered_df = df.loc[~df['ts_code'].str.startswith(('864', '866'))]
```
其中,~ 表示取反,即排除 ts_code 列开头为 864 或 866 的行。str.startswith 函数可以传入一个元组,表示多个前缀。
最后,您可以使用 Pandas 的 to_excel 函数将过滤后的数据保存到新的 Excel 文件中,例如:
```python
filtered_df.to_excel('filtered_file.xlsx', index=False)
```
其中,index 参数为 False 表示不保存行索引。
希望这些代码可以帮助您实现您的要求!
python df中ts_code列值开头为864的不保存
如果您想要实现这个功能,您可以使用 Pandas 库来读取和处理表格数据。
首先,您可以使用 Pandas 的 read_excel 函数读取 Excel 文件,例如:
```python
import pandas as pd
df = pd.read_excel('file.xlsx')
```
然后,您可以使用 Pandas 的 loc 函数和字符串方法 str.startswith 来选取 ts_code 列开头不为 864 的行,例如:
```python
filtered_df = df.loc[~df['ts_code'].str.startswith('864')]
```
其中,~ 表示取反,即排除 ts_code 列开头为 864 的行。
最后,您可以使用 Pandas 的 to_excel 函数将过滤后的数据保存到新的 Excel 文件中,例如:
```python
filtered_df.to_excel('filtered_file.xlsx', index=False)
```
其中,index 参数为 False 表示不保存行索引。
希望这些代码可以帮助您实现您的要求!
相关推荐
![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)