Pandas 中允许重复列

2024-05-20

我将一个大的 CSV(包含股票财务数据)文件分割成更小的块。 CSV 文件的格式不同。像 Excel 数据透视表之类的东西。第一列的前几行包含一些标题。

公司名称、ID 等在以下列中重复。因为一家公司有多个属性,而不是一家公司只有一栏。

在前几行之后,列开始类似于典型的数据帧,其中标题位于列而不是行中。

无论如何,我想做的是让 Pandas 允许重复的列标题,而不是让它在标题后添加“.1”、“.2”、“.3”等。我知道 Pandas 本身不允许这样做,有解决方法吗?我尝试在 read_csv 上设置 header = None 但它引发了一个标记化错误,我认为这是有道理的。我只是想不出一个简单的方法。

import pandas as pd

csv_path = "C:\\Users\\ThirdHandBD\\Desktop\\Data Splitting\\pd-split\\chunk4.csv"

#df = pd.read_csv(csv_path, header=1, dtype='unicode', sep=';', low_memory=False, error_bad_lines=False)
df = pd.read_csv(csv_path, header = 1, dtype='unicode', sep=';', index_col=False)
print("I read in a dataframe with {} columns and {} rows.".format(
len(df.columns), len(df)
))

filename = 1

#column increment
x = 30 * 59

for column in df:
    loc = df.columns.get_loc(column)
    if loc == (x * filename) + 1:
        y = filename - 1
        a = (x * y) + 1
        b = (x * filename) + 1
        date_df = df.iloc[:, :1]
        out_df = df.iloc[:, a:b]
        final_df = pd.concat([date_df, out_df], axis=1, join='inner')
        out_path = "C:\\Users\\ThirdHandBD\\Desktop\\Data Splitting\\pd-split\\chunk4-part" + str(filename) + ".csv"
        final_df.to_csv(out_path, index=False)
        #out_df.to_csv(out_path)
        filename += 1

# This should be the same as df, but with only the first column.
# Check it with similar code to above.

EDIT:

From, https://github.com/pandas-dev/pandas/issues/19383 https://github.com/pandas-dev/pandas/issues/19383, I add:

        final_df.columns = final_df.iloc[0]
        final_df = final_df.reindex(final_df.index.drop(0)).reset_index(drop=True)

所以,完整代码:

import pandas as pd

csv_path = "C:\\Users\\ThirdHandBD\\Desktop\\Data Splitting\\pd-split\\chunk4.csv"

#df = pd.read_csv(csv_path, header=1, dtype='unicode', sep=';', low_memory=False, error_bad_lines=False)
df = pd.read_csv(csv_path, header = 1, dtype='unicode', sep=';', index_col=False)
print("I read in a dataframe with {} columns and {} rows.".format(
len(df.columns), len(df)
))

filename = 1

#column increment
x = 30 * 59

for column in df:
    loc = df.columns.get_loc(column)
    if loc == (x * filename) + 1:
        y = filename - 1
        a = (x * y) + 1
        b = (x * filename) + 1
        date_df = df.iloc[:, :1]
        out_df = df.iloc[:, a:b]
        final_df = pd.concat([date_df, out_df], axis=1, join='inner')
        out_path = "C:\\Users\\ThirdHandBD\\Desktop\\Data Splitting\\pd-split\\chunk4-part" + str(filename) + ".csv"
        final_df.columns = final_df.iloc[0]
        final_df = final_df.reindex(final_df.index.drop(0)).reset_index(drop=True)
        final_df.to_csv(out_path, index=False)
        #out_df.to_csv(out_path)
        filename += 1

# This should be the same as df, but with only the first column.
# Check it with similar code to above.

现在,整个第一行都消失了。但是,预期输出是将标题行替换为重置索引,而不包含“.1”、“.2”等。

截屏:

SimFin ID 行不再存在。


我就是这样做的:

    final_df.columns = final_df.columns.str.split('.').str[0]

参考:https://pandas.pydata.org/pandas-docs/stable/text.html https://pandas.pydata.org/pandas-docs/stable/text.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pandas 中允许重复列 的相关文章

随机推荐