Я работаю с огромным CSV-файлом (873,323 x 271), который выглядит примерно так, как показано ниже:
| Part_Number | Type_Code | Building_Code | Handling_Code | Price to Buy | Price to Sell | Name |
|:-----------:|:-------------:|:--------------:|:-------------:|:------------:|:-------------:|:-------------:|
| A | 1, 2 | XX, XX, XX | Y, Y, Y, Y, Y | 304.32 | 510 | Mower |
| B | 1, 1, 1 | XX, XX, XX | Y, Y, Y, Y | 1282.04 | 5000 | Saw |
| C | 1, 2, 3 | XX, XX | Y, Y | 68.91 | 65 | Barrel (Hard) |
| D | 1, 1, 1, 1, 1 | XX, XX, XX, XX | Y, Y, Y | 0 | 300 | Barrel (Make) |
| E | 1 | XX | Y, Y, Y, Y | 321.11 | 415 | Cement Mixer |
| F | 2 | XX, XX, XX | Y | 194.44 | 1095 | Cement Mix |
Существует несколько типов столбцов: некоторые числовые, некоторые строковые, и некоторые строки выглядят как списки (например, Type_Code
, Building_Code
, Handling_Code
, et c.)
То, что я пытаюсь сделать sh:
Если каждое значение в столбце является одним и тем же значением, удалите подобную списку структуру и замените ее только этим значением. т. е. 1, 1, 1 должно стать равным 1. Числовые и не похожие на списки строки не должны изменяться
Изменяя приведенную выше таблицу:
| Part_Number | Type_Code | Building_Code | Handling_Code | Price to Buy | Price to Sell | Name |
|:-----------:|:---------:|:-------------:|:-------------:|:------------:|:-------------:|:-------------:|
| A | 1, 2 | XX | Y | 304.32 | 510 | Mower |
| B | 1 | XX | Y | 1282.04 | 5000 | Saw |
| C | 1, 2, 3 | XX | Y | 68.91 | 65 | Barrel (Hard) |
| D | 1 | XX | Y | 0 | 300 | Barrel (Make) |
| E | 1 | XX | Y | 321.11 | 415 | Cement Mixer |
| F | 2 | XX | Y | 194.44 | 1095 | Cement Mix |
(т. е. поскольку Building_Code
был просто совокупностью XX
, он должен просто сказать XX
)
Ниже моя текущая попытка:
import pandas as pd
# Read in CSV
df = pd.read_csv('C:\\Users\\wundermahn\\Desktop\\test_stack_csv.csv')
# Turn all columns into a list
for col in df.columns:
col_name = str(col)
temp = pd.DataFrame(df[col_name].tolist())
df.drop(col, axis=1, inplace=True)
df = pd.concat([df, temp], axis=1, join='inner')
# Now loop through the columns and remove items from the list
for col in df.columns:
# If all items are the same
if (len(set(col)) <= 1):
# Set it to be that item
col = col[0]
else:
# If they aren't the same, then just take the items out of the list
col = str(col)
print(df)
Но я получаю ошибку:
Traceback (most recent call last):
File "c:\Users\wundermahn\Desktop\stack_0318.py", line 15, in <module>
if (len(set(col)) <= 1):
TypeError: 'int' object is not iterable
Как мне достичь желаемого результата?