在 Python 3.9 中聚合 CSV 数据的最简单方法是什么?
What's the easiest way to aggregate my CSV data in Python 3.9?
我正在使用 Python 3.9。我正在尝试解析这个包含 3 列数据的 CSV 文件
55,Fake ISD,SUCCESS
56,Other ISD,None
57,Third ISD,WARNING
58,Fourth ISD,FAILURE
59,Main ISD,SUCCESS
60,Secondary ISD,SUCCESS
我想知道是否有一些开箱即用的库可以解析 CSV 以根据第三列的结果聚合数据。也就是说,我想要一份报告
SUCCESS - 3 entries - Fake ISD, Main ISD, Secondary ISD
WARNING - 1 entry - Third ISD
FAILURE - 1 entry - Fourth ISD
None - 1 entry - Other ISD
我如何将这些汇总到 Python 3.9 中?
你可以试试pandas
:
import pandas as pd
df = pd.read_csv("your_file.csv", header=None)
x = df.groupby(2)[1].agg(list)
for i, d in zip(x.index, x):
print(f'{i} - {len(d)} - {", ".join(d)}')
打印:
FAILURE - 1 - Fourth ISD
None - 1 - Other ISD
SUCCESS - 3 - Fake ISD, Main ISD, Secondary ISD
WARNING - 1 - Third ISD
我正在使用 Python 3.9。我正在尝试解析这个包含 3 列数据的 CSV 文件
55,Fake ISD,SUCCESS
56,Other ISD,None
57,Third ISD,WARNING
58,Fourth ISD,FAILURE
59,Main ISD,SUCCESS
60,Secondary ISD,SUCCESS
我想知道是否有一些开箱即用的库可以解析 CSV 以根据第三列的结果聚合数据。也就是说,我想要一份报告
SUCCESS - 3 entries - Fake ISD, Main ISD, Secondary ISD
WARNING - 1 entry - Third ISD
FAILURE - 1 entry - Fourth ISD
None - 1 entry - Other ISD
我如何将这些汇总到 Python 3.9 中?
你可以试试pandas
:
import pandas as pd
df = pd.read_csv("your_file.csv", header=None)
x = df.groupby(2)[1].agg(list)
for i, d in zip(x.index, x):
print(f'{i} - {len(d)} - {", ".join(d)}')
打印:
FAILURE - 1 - Fourth ISD
None - 1 - Other ISD
SUCCESS - 3 - Fake ISD, Main ISD, Secondary ISD
WARNING - 1 - Third ISD