【问题标题】:Sorting and splitting csv排序和拆分csv
【发布时间】:2022-01-08 06:17:53
【问题描述】:

我有一个 csv 文件如下:

Weather

我需要将 csv 文件拆分为多个列表,其中包含 Timestart 相等的元素。使用下面的代码,但它不起作用。

from numpy import e, not_equal
import pandas as pd
import csv
data= pd.read_csv("testing.csv")
t = data.sort_values('Timestart')

for x in range(len(t.Timestart)):
   for y in range(1+len(t.Timestart)):
      if t.Timestart[x] == t.Timestart[y]:
         print('Vin with same timewindow is: ',t.Vin[y])

【问题讨论】:

标签: python csv


【解决方案1】:

这可以通过使用 pandas 的 groupby 或过滤功能方便地实现。查看使用过滤的解决方案,您可以通过pandas.Series.unique() 检索所有唯一列值。使用这些唯一值,您可以使用带有布尔表达式的函数 .loc[] 按列 Timestart 中的值过滤数据框。

在下面的示例中,过滤后的结果存储在名为data 的字典中,当然也可以是嵌套列表等。

import pandas as pd

# Reads the csv-file
df = pd.read_csv("testing.csv")

# Find all values in the Timestart-column
timestart_unique = df.Timestart.unique()

# dict indexed by unique Timestart-values
data = {}  

# Populates the dict by filtering
for t in timestart_unique:
    data[t] = df.loc[df.Timestart == t]

访问data[1800] 会得到类似的东西:


    id  Vin     Make    Timestart   Timestop
0   1   12345   London  1800    1845
1   2   23456       NY  1800    1845
3   4   345678  London  1800    1845

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2013-10-13
    • 2021-02-17
    • 2012-11-20
    • 2018-05-26
    • 2019-10-09
    相关资源
    最近更新 更多