【问题标题】:There should be a word or value in a dataframe, but everything is being displayed as NaN数据框中应该有一个单词或值,但所有内容都显示为 NaN
【发布时间】:2019-10-24 06:30:14
【问题描述】:

我正在尝试将 199 个 txt 文件合并到一个具有相同列的文件中。我能够调用它们并合并它们,但是当我尝试合并列时,所有值都变为“NaN”。共有 3 列:名称、性别和出现次数。如何获得要显示的正确单词/值?

import pandas as pd
import glob

path = 'data/names/' 
all_files = glob.glob(path + "/*.txt")

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)
print (all_files)
frame = pd.concat(li, axis=0, ignore_index=True)

df_a = pd.DataFrame(frame, columns = ['Name', 'Gender', 'Occurance'])
df_a

输出应该有一个名字、一个性别(“女性”或“男性”)和一个与出现相关的数字,而不是所有内容的“NaN”。

【问题讨论】:

    标签: python pandas merge concatenation nan


    【解决方案1】:

    您需要确保所有数据框具有相同的列,否则

    交叉点外的列将填充 NaN 值

    (见https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.concat.html

    【讨论】:

      猜你喜欢
      • 2017-09-11
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2013-06-22
      相关资源
      最近更新 更多