dataframe读取一列同时有字符串和日期的数据时,df = pd.read_excel('C:/Users/wei_li4/Downloads/PD佳佳/旧文件 改.xlsx', sheet_name='PD report', dtype={'date_column': str}),如何能使读出来的日期格式不包括时间?
时间: 2023-06-04 11:08:41 浏览: 71
你可以在读取之后使用pandas中的to_datetime函数将日期列转换为日期格式,然后再使用strftime函数将日期格式化为你需要的形式,如下所示:
```
import pandas as pd
df = pd.read_excel('C:/Users/wei_li4/Downloads/PD佳佳/旧文件 改.xlsx', sheet_name='PD report')
df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'] = df['date_column'].dt.strftime('%Y-%m-%d')
```
这样就可以将日期格式格式化为年-月-日的形式,不包括时间。
相关问题
import pandas as pd df1=pd.read_csv('/Users/hiedihe/Desktop/hao/斗破苍穹.csv') df2=pd.read_csv('/Users/hiedihe/Desktop/hao/爱消除表格新.csv') # 合并两个DataFrame merged_df = pd.concat([df1, df2]) # 将合并后的数据保存为新的Excel文件 merged_df.to_csv('he11.xlsx', index=False) 报错:File "/Applications/pythonProject/main.py", line 3 df1=pd.read_csv('/Users/hiedihe/Desktop/hao/斗破苍穹.csv') IndentationError: unexpected indent
这个错误是缩进错误(IndentationError),意味着您的代码缩进不正确。在Python中,缩进对于代码的结构和执行非常重要。请确保您的代码块有正确的缩进。
在您提供的代码中,错误出现在第3行。请检查该行及其之前的行是否有额外的缩进或不正确的缩进。您可以使用四个空格或一个制表符来进行缩进,但不能混合使用。
以下是修正后的代码示例:
```python
import pandas as pd
df1 = pd.read_csv('/Users/hiedihe/Desktop/hao/斗破苍穹.csv')
df2 = pd.read_csv('/Users/hiedihe/Desktop/hao/爱消除表格新.csv')
# 合并两个DataFrame
merged_df = pd.concat([df1, df2])
# 将合并后的数据保存为新的Excel文件
merged_df.to_csv('he11.xlsx', index=False)
```
请确保代码的每一行都有正确的缩进,并且行与行之间没有额外的缩进或缺少缩进。如果仍然有问题,请检查其他部分的缩进。
import pandas as pd df = pd.read_excel('./train.xls') df_test = pd.read_excel('./test.xls')
这段代码使用了 Pandas 库来读取 Excel 文件。
首先,通过 `pd.read_excel` 函数读取名为 `'train.xls'` 的 Excel 文件,并将其内容存储在名为 `df` 的 DataFrame 中。
接着,通过 `pd.read_excel` 函数读取名为 `'test.xls'` 的 Excel 文件,并将其内容存储在名为 `df_test` 的 DataFrame 中。
这段代码的作用是读取 Excel 文件的内容,并将其转换为 Pandas 的 DataFrame 格式,以便后续的数据处理和分析。请确保文件路径正确,并且已经安装了 Pandas 库。
相关推荐
![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)