我有一個 csv 檔案,如下所示:
天氣
我需要將 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])
uj5u.com熱心網友回復:
這可以通過使用 pandas 的 groupby 或過濾功能方便地實作。查看使用過濾的解決方案,您可以通過pandas.Series.unique(). 使用這些唯一值,您可以Timestart使用.loc[]帶有布爾運算式的函式按列中的值過濾資料框。
在以下示例中,過濾后的結果存盤在名為 的 dictdata中,當然也可以是嵌套串列等。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/407259.html
標籤:
