gpt4 book ai didi

python - 为没有名称的数据框列命名

转载 作者:行者123 更新时间:2023-12-01 00:46:06 25 4
gpt4 key购买 nike

我将 pd.series 转换为数据帧。转换后,数据帧列之一没有任何名称,另一列的名称为“0”。我需要为该列命名。

我尝试使用 df.columns = ["A","B"] 并重命名,但没有帮助

import pandas as pd
import nltk
from nltk.corpus import stopwords #for removing stopwords
import re #for removing numbers, special characters
#Import CSV into dataframe
filepath = "C:/a/Python/Clustering/LabeledRawDatav2.csv"
df = pd.read_csv(filepath,encoding='windows-1252')
print(df.head(2))

freq = pd.DataFrame(columns=["Word","Count"])

freq = pd.Series(' '.join(df["Notes"]).split()).value_counts()[:]
freq = pd.Series.to_frame(freq)

freq.rename(columns = {"0":"Freq"},inplace=True)

print(freq)

预期结果是

Word                  freq
- 206
the 65
for 62
1 62
DAYS 56

实际结果是

                        0
- 206
the 65
for 62
1 62
DAYS 56

最佳答案

我通常这样做:

freq = df["Notes"].str.split(expand = True).stack().value_counts().rename_axis('word').reset_index(name = 'count')

这可以克服0列问题。

致原作者jezrael因为我是从他的一个答案中获取的,似乎找不到原始链接!

关于python - 为没有名称的数据框列命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56970665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com