def save_to_sql(): df = ps.read_csv("./datas.csv",index_col=0) df.to_sql('movies_cop',con=engine,index=False,if_exists ='append')
时间: 2024-06-01 22:10:17 浏览: 9
This function reads a CSV file named "datas.csv" and saves its contents to a SQL database table named "movies_cop". The function uses the pandas library to read the CSV file and convert it into a pandas DataFrame. It then uses the to_sql method to save the DataFrame to the SQL database using the provided SQLalchemy engine. The if_exists parameter is set to 'append', which means that if the table already exists, the data will be added to the existing data.
相关问题
datas = pd.read_csv("/home/zyy/Atime/Autoformer_me/data/7m.csv") AttributeError: 'numpy.ndarray' object has no attribute 'read_csv'怎么解决
这个错误的原因是使用了`numpy.ndarray`对象调用了`read_csv`方法,而`read_csv`方法是`pandas`库中的一个函数,所以需要将数据类型转换为`pandas`的`DataFrame`对象。
你可以尝试将数据读取为`DataFrame`对象,然后再进行相关操作。具体代码如下:
```python
import pandas as pd
# 读取csv文件为DataFrame对象
datas = pd.read_csv('/home/zyy/Atime/Autoformer_me/data/7m.csv')
# 进行相关操作
# ...
# 将修改后的数据写入新的csv文件
datas.to_csv('/home/zyy/Atime/Autoformer_me/data/new_7m.csv', index=False)
```
如果你已经有了`numpy.ndarray`对象,可以使用`pandas`中的`DataFrame`函数将其转换为`DataFrame`对象,具体代码如下:
```python
import pandas as pd
import numpy as np
# 生成numpy.ndarray对象
arr = np.array([[1, 2], [3, 4]])
# 将numpy.ndarray对象转换为DataFrame对象
datas = pd.DataFrame(arr)
# 进行相关操作
# ...
# 将修改后的数据写入新的csv文件
datas.to_csv('/home/zyy/Atime/Autoformer_me/data/new_7m.csv', index=False)
```
希望这些代码可以帮助到你。
try: df = pd.concat(Datas,ignore_index=False) df.drop_duplicates([ ‘比赛编号’, ‘判断’,‘半场比分’,‘比分’], keep=‘first’, inplace=True)
这段代码使用了Pandas库中的concat()和drop_duplicates()函数。其中,concat()函数可以将多个数据集按照一定的方式拼接起来,生成一个新的数据集;而drop_duplicates()函数可以根据指定的列名,去除数据集中的重复项。
具体来说,这段代码首先使用concat()函数将列表Datas中的多个数据集按照行的方式进行拼接,生成一个名为df的新数据集。然后,使用drop_duplicates()函数对df数据集进行处理,去除其中指定列(‘比赛编号’, ‘判断’,‘半场比分’,‘比分’)上的重复项,并保留第一次出现的记录。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)