[英]How to iterate through CSV hierarchy tree in Python?
我會使用pandas
。
import pandas as pd
df = pd.read_csv(path_to_csv)
# Fill empty cells in parent column with the precedent value
df['parent'] = df.parent.fillna(method='ffill')
# Group cells with the same parent
df = df.groupby('parent').agg({'child': list})
print(df)
print(df.loc['Apple'])
這將最終得到一個由父級索引的表,它們的子級分組到列表結構中的單個單元格中。 接下來,您可以對每個列表執行任何操作,甚至可以將 pandas dataframe (=table) 轉換為最適合您情況的任何其他內容 (list, dict...)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.