如何将 collections.Counter 转换为 pandas DataFrame
Pandas 可以自行处理 Counter 到 DataFrame 的转换,但你需要添加列标签:
counter_to_dataframe.py
pd.DataFrame({"YourColumnLabelGoesHere": counterObject})完整示例
counter_to_dataframe_full.py
import pandas as pd
from collections import Counter
ctr = Counter()
ctr["a"] += 1
ctr["b"] += 1
ctr["a"] += 1
ctr["a"] += 1
ctr["b"] += 1
ctr["a"] += 1
ctr["c"] += 1
pd.DataFrame({"ctr": ctr})这将产生以下 DataFrame:

If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow