gpt4 book ai didi

python - Pandas : Get Data from a csv file irrespective of Header position

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:36 25 4
gpt4 key购买 nike

我有 csv 文件,我必须用 panda 制作dataframes。通常文件的格式如下:

Days    Page Impressions    Visits  Bounces
2012-12-15 692041 87973 31500
2012-12-16 602356 78663 29298
2012-12-17 730902 99356 37436
2012-12-18 730071 97844 37199
2012-12-19 774964 110446 43858
2012-12-20 419256 44592 13961
2012-12-21 320966 33692 10076
2012-12-22 200992 18840 5170


我面临的问题是有时 CSV 文件是这样的:

SomeName ABCD           
Account: AccountHolder Name
Report Author: Analysis
Description: Some variable length description

Pivot

Pivot
Days Page Impressions Visits Bounces
2012-12-15 367143 69147 30222
2012-12-16 334675 63702 28040
2012-12-17 409260 77171 33642
2012-12-18 427765 78221 33575
2012-12-19 434781 79850 34300
2012-12-20 463448 81361 34501
2012-12-21 447964 81897 35242
2012-12-22 368477 70352 31014
2012-12-23 321891 61973 27521

Time of Calculation: 2013-03-15 02:14:58

如何才能只获取与天数、页面印象数、访问数、跳出数

列相关联的数据

我知道我可以执行 list(my_dataframe.columns.values) 来获取 header 名称,但它不适用于 CSV 文件类型 2。

他们是否有任何现有的 Pythonic 方式在 pandas 中执行此操作?
谢谢

最佳答案

分两次读取文件。首先枚举文件中的行以获取标题所在的行。将该行号传递给 csv 解析器 skiprows arg。

with open('file.csv', 'rb') as infile:
for lineno, line in enumerate(infile):
if line[:4] = 'Days':
break

df = pd.read_csv('file.csv', skiprows=lineno)

关于python - Pandas : Get Data from a csv file irrespective of Header position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com