パースパンダのデータフレームの列は、同じ値をチェックします

wundermahn:

私は以下であるものに似ています巨大なcsvファイル(873323 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_CodeBuilding_CodeHandling_Code、など)

私は何を達成しようとしていることです。

列の各値が同じ値である場合は、リストのような構造を削除し、ちょうどその値で置き換えます。すなわち、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

どのように私は私の望ましい結果を達成することができますか?

anky_91:

分割カスタム関数のようなこのルックス,私が使用しているため、重複を削除した後、それをバック合流し、dict.fromkeys

f = lambda x:','.join(dict.fromkeys([i.strip() for i in x.split(',')]).keys())

df.loc[:,df.dtypes.eq('object')]=df.select_dtypes('O').applymap(f)

print(df)

   Part_Number Type_Code Building_Code Handling_Code  Price to Buy  \
0           A       1,2            XX             Y        304.32   
1           B         1            XX             Y       1282.04   
2           C     1,2,3            XX             Y         68.91   
3           D         1            XX             Y          0.00   
4           E         1            XX             Y        321.11   
5           F         2            XX             Y        194.44   

   Price to Sell           Name  
0            510          Mower  
1           5000            Saw  
2             65  Barrel (Hard)  
3            300  Barrel (Make)  
4            415   Cement Mixer  
5           1095     Cement Mix  

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=320263&siteId=1