如何让pandas在输出中显示大型数据集?

0 投票
2 回答
45 浏览
提问于 2025-04-11 21:50

我正在处理一个非常大的Excel数据集,里面有超过十万行的数据,包含了小时和日期的信息,但这些数据没有被分开,比如日期是20231201,而不是2023/12/01,时间是1130,而不是11:30。我写了一段代码来把这些数据分开,以便可以复制粘贴回Excel,但输出的结果总是缺少前面三万行数据……有没有办法把输出的行数设置为无限呢?

#this is the code for hours
import pandas as pd

df = pd.read_excel('/Volumes/PortableSSD/Università - Lavori/Progetto statistica/Definitivo 1223.xlsx')

df['Scheduled departure'] = df['Scheduled departure'].astype(str)

df['formatted_hour'] = df['Scheduled departure'].apply(lambda x: '{:0>4}'.format(x))

df['formatted_hour'] = df['formatted_hour'].apply(lambda x: f"{x[:2]}:{x[2:]}")

# Display the formatted time
print(df['formatted_hour'].to_string(index=True))
#this is the code for dates
import pandas as pd

df = pd.read_excel('/Volumes/PortableSSD/Università - Lavori/Progetto statistica/Definitivo 1223.xlsx')

df['Date'] = df['Date'].astype(str)
df['year'] = df['Date'].str[:4]
df['month'] = df['Date'].str[4:6]
df['day'] = df['Date'].str[6:]

df['formatted_date'] = df['Date'].str[6:] + '/' + df['Date'].str[4:6] + '/' + df['Date'].str[:4]

# Display the formatted date
print(df['formatted_date'].to_string(index=False))

2 个回答

0

你为什么不按照你的需求来格式化这些列,然后把格式化后的内容覆盖到你的文件里呢?

df.to_excel(<path to file>, index = False)
0

不幸的是,所有的集成开发环境(IDE)在显示数据集时都有一些限制。不过,你可以一行一行地打印,直到你把整个数据表都显示完。

撰写回答