Python, Pandas удаляет столбец с данными и вставляет пустую строку
Столкнулся с проблемой: при создании фрейма данных pandas удаляет один столбец с данными и вставляет вторую строку с пустыми значениями. Как это исправить?
import pandas as pd
import json
from io import StringIO
List_Objects = [{'CadastralNumber': '37:24:01024'},
{'DateCreated': '2011-12-09+04:00'},
{'FoundationDate': ''},
{'Area': '5462'},
{'AssignationBuilding': '204003000000'},
{'CadastralBlock': '37:24:010246'},
{'CadastralCost': ''},
{'CadastralCost_Value': '163292388.96'},
{'CadastralCost/ApplicationDate': ''},
{'CadastralCost/ApplicationLastDate': ''},
{'CadastralCost/ApprovalDocument': ''},
{'CadastralCost/ApprovalDocument/CodeDocument': ''},
{'CadastralCost/ApprovalDocument/Date': ''},
{'CadastralCost/ApprovalDocument/Desc': ''},
{'CadastralCost/ApprovalDocument/IssueOrgan': ''},
{'CadastralCost/ApprovalDocument/Name': ''},
{'CadastralCost/ApprovalDocument/Number': ''},
{'CadastralCost/ApprovalDocument/Series': ''},
{'CadastralCost/DateApproval': '2012-09-27+04:00'},
{'CadastralCost/DateEntering': '2012-10-09+04:00'},
{'CadastralCost/DateValuation': '2011-12-10+04:00'},
{'CadastralCost/RevisalStatementDate': ''},
{'CarParkingSpacesCadastralNumbers': ''},
{'CarParkingSpacesCadastralNumbers/CadastralNumber': ''},
{'CulturalHeritage': ''},
{'CulturalHeritage/AssignmentEGROKN': ''},
{'CulturalHeritage/AssignmentEGROKN/NameCultural': ''},
{'CulturalHeritage/AssignmentEGROKN/ObjCultural': ''},
{'CulturalHeritage/AssignmentEGROKN/RegNum': ''},
{'CulturalHeritage/Document': ''},
{'CulturalHeritage/Document/CodeDocument': ''},
{'CulturalHeritage/Document/Date': ''},
{'CulturalHeritage/Document/Desc': ''},
{'CulturalHeritage/Document/IssueOrgan': ''},
{'CulturalHeritage/Document/Name': ''},
{'CulturalHeritage/Document/Number': ''},
{'CulturalHeritage/Document/Series': ''},
{'CulturalHeritage/InclusionEGROKN': ''},
{'CulturalHeritage/InclusionEGROKN/NameCultural': ''},
{'CulturalHeritage/InclusionEGROKN/ObjCultural': ''},
{'CulturalHeritage/InclusionEGROKN/RegNum': ''},
{'CulturalHeritage/RequirementsEnsure': ''},
{'ElementsConstruct': ''},
{'ElementsConstruct/Material': None},
{'ElementsConstruct/Material_Wall': '061001007001'},
{'ExploitationChar': None},
{'ExploitationChar_YearBuilt': '1978'},
{'ExploitationChar_YearUsed': ''},
{'FacilityCadastralNumber': ''},
{'FacilityCadastralNumber/CadastralNumber': ''},
{'FacilityCadastralNumber/Name': ''},
{'FacilityCadastralNumber/Purpose': ''},
{'FlatsCadastralNumbers': ''},
{'FlatsCadastralNumbers/CadastralNumber': '37:24'},
{'Floors': None},
{'Floors_Floors': '6'},
{'Floors_UndergroundFloors': '1'},
{'Location': ''},
{'Location_AddressOrLocation': ''},
{'Location/Apartment': ''},
{'Location/Apartment_Type': ''},
{'Location/Apartment_Value': ''},
{'Location/City': None},
{'Location/City_Name': 'Иваново'},
{'Location/City_Type': 'Г'},
{'Location/District': ''},
{'Location/District_Name': ''},
{'Location/District_Type': ''},
{'Location/FIAS': ''},
{'Location/KLADR': '37000001000074600'},
{'Location/Level1': None},
{'Location/Level1_Type': 'Д.'},
{'Location/Level1_Value': '35'},
{'Location/Level2': ''},
{'Location/Level2_Type': ''},
{'Location/Level2_Value': ''},
{'Location/Level3': ''},
{'Location/Level3_Type': ''},
{'Location/Level3_Value': ''},
{'Location/Locality': ''},
{'Location/Locality_Name': ''},
{'Location/Locality_Type': ''},
{'Location/Note': ''},
{'Location/OKATO': '24401000000'},
{'Location/OKTMO': ''},
{'Location/Other': ''},
{'Location/PostalCode': ''},
{'Location/ReadableAddress': ''},
{'Location/Region': '37'},
{'Location/RussianFederation': 'Российская Федерация'},
{'Location/SovietVillage': ''},
{'Location/SovietVillage_Name': ''},
{'Location/SovietVillage_Type': ''},
{'Location/Street': None},
{'Location/Street_Name': ''},
{'Location/Street_Type': 'Ул'},
{'Location/UrbanDistrict': ''},
{'Location/UrbanDistrict_Name': ''},
{'Location/UrbanDistrict_Type': ''},
{'Name': 'Многоквартирный Дом'},
{'ObjectPermittedUses': ''},
{'ObjectPermittedUses/ObjectPermittedUse': ''},
{'ObjectType': '002001002000'},
{'ParentCadastralNumbers': ''},
{'ParentCadastralNumbers/CadastralNumber': '37:24:010'},
{'PrevCadastralNumbers': ''},
{'PrevCadastralNumbers/CadastralNumber': ''},
{'PreviouslyPosted': 'True'},
{'SubBuildings': ''},
{'SubBuildings/SubBuilding': ''},
{'SubBuildings/SubBuilding_NumberRecord': ''},
{'SubBuildings/SubBuilding_DateCreated': ''},
{'SubBuildings/SubBuilding/Area': ''},
{'SubBuildings/SubBuilding/Encumbrances': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/CodeDocument': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/Date': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/Desc': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/IssueOrgan': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/Name': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/Number': ''},
{'SubBuildings/SubBuilding/Encumbrances/Document/Series': ''},
{'SubBuildings/SubBuilding/Encumbrances/Name': ''},
{'SubBuildings/SubBuilding/Encumbrances/Registration': ''},
{'SubBuildings/SubBuilding/Encumbrances/Registration/RegistrationDate': ''},
{'SubBuildings/SubBuilding/Encumbrances/Registration/RightNumber': ''},
{'SubBuildings/SubBuilding/Encumbrances/Type': ''},
{'UnitedCadastralNumber': ''},
{'UnitedCadastralNumber/CadastralNumber': ''},
{'UnitedCadastralNumber/Name': ''},
{'UnitedCadastralNumber/Purpose': ''},
{'CadastralNumberOKS': ''},
{'ParentOKS': ''},
{'ParentOKS/AssignationBuilding': ''},
{'ParentOKS/AssignationName': ''},
{'ParentOKS/CadastralNumberOKS': ''},
{'ParentOKS/ElementsConstruct': ''},
{'ParentOKS/ElementsConstruct/Material': ''},
{'ParentOKS/ElementsConstruct/Material_Wall': ''},
{'ParentOKS/ExploitationChar': ''},
{'ParentOKS/ExploitationChar_YearBuilt': ''},
{'ParentOKS/ExploitationChar_YearUsed': ''},
{'ParentOKS/Floors': ''},
{'ParentOKS/Floors_Floors': ''},
{'ParentOKS/Floors_UndergroundFloors': ''},
{'ParentOKS/ObjectType': ''},
{'PositionInObject': ''},
{'PositionInObject_Number': ''},
{'PositionInObject_Type': ''},
{'PositionInObject/Position': ''},
{'PositionInObject/Position_NumberOnPlan': ''},
{'PositionInObject/Position_Description': ''},
{'AssignationName': ''},
{'KeyParameters': ''},
{'KeyParameters/KeyParameter': ''},
{'KeyParameters/KeyParameter_Type': ''},
{'KeyParameters/KeyParameter_Value': ''},
{'SubConstructions': ''},
{'SubConstructions/SubConstruction': ''},
{'SubConstructions/SubConstruction_NumberRecord': ''},
{'SubConstructions/SubConstruction_DateCreated': ''},
{'SubConstructions/SubConstruction/Encumbrances': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/CodeDocument': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/Date': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/Desc': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/IssueOrgan': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/Name': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/Number': ''},
{'SubConstructions/SubConstruction/Encumbrances/Document/Series': ''},
{'SubConstructions/SubConstruction/Encumbrances/Name': ''},
{'SubConstructions/SubConstruction/Encumbrances/Registration': ''},
{'SubConstructions/SubConstruction/Encumbrances/Registration/RegistrationDate': ''},
{'SubConstructions/SubConstruction/Encumbrances/Registration/RightNumber': ''},
{'SubConstructions/SubConstruction/Encumbrances/Type': ''},
{'SubConstructions/SubConstruction/KeyParameter': ''},
{'SubConstructions/SubConstruction/KeyParameter_Type': ''},
{'SubConstructions/SubConstruction/KeyParameter_Value': ''},
{'Assignation': ''},
{'Assignation/AssignationCode': ''},
{'Assignation/AssignationType': ''},
{'Assignation/AuxiliaryFlat': ''},
{'Assignation/SpecialType': ''},
{'Assignation/TotalAssets': ''},
{'CadastralNumberFlat': ''},
{'Location/RoomNumber': ''},
{'PositionInObject': ''},
{'PositionInObject/Level': ''},
{'PositionInObject/Level_Number': ''},
{'PositionInObject/Level_Type': ''},
{'PositionInObject/Level/Position': ''},
{'PositionInObject/Level/Position_NumberOnPlan': ''},
{'PositionInObject/Level/Position_Description': ''},
{'SubFlats': ''},
{'SubFlats/SubFlat': ''},
{'SubFlats/SubFlat_NumberRecord': ''},
{'SubFlats/SubFlat_DateCreated': ''},
{'SubFlats/SubFlat/Area': ''},
{'SubFlats/SubFlat/Encumbrances': ''},
{'SubFlats/SubFlat/Encumbrances/Document': ''},
{'SubFlats/SubFlat/Encumbrances/Document/CodeDocument': ''},
{'SubFlats/SubFlat/Encumbrances/Document/Date': ''},
{'SubFlats/SubFlat/Encumbrances/Document/Desc': ''},
{'SubFlats/SubFlat/Encumbrances/Document/IssueOrgan': ''},
{'SubFlats/SubFlat/Encumbrances/Document/Name': ''},
{'SubFlats/SubFlat/Encumbrances/Document/Number': ''},
{'SubFlats/SubFlat/Encumbrances/Document/Series': ''},
{'SubFlats/SubFlat/Encumbrances/Name': ''},
{'SubFlats/SubFlat/Encumbrances/Registration': ''},
{'SubFlats/SubFlat/Encumbrances/Registration/RegistrationDate': ''},
{'SubFlats/SubFlat/Encumbrances/Registration/RightNumber': ''},
{'SubFlats/SubFlat/Encumbrances/Type': ''},
{'DegreeReadiness': ''}]
json_data = json.dumps(List_Objects)
df = pd.read_json(StringIO(json_data))
df = df.apply(lambda x: pd.Series(x.dropna().values))
print(df)
print(len(List_Objects))
CadastralNumber ... DegreeReadiness
0 37:24:010246:397 ...
1 NaN ... NaN
[2 rows x 212 columns]
213
Process finished with exit code 0
Ответы (1 шт):
Автор решения: strawdog
→ Ссылка
вы методом .dropna
удаляете все nan в каждом столбце, затем "сжимая" эти столбцы с помощью pd.Series
до значимых ячеек. а значимых ячеек у вас везде по одной (отсюда первая строка в результате), кроме столбца PositionInObject
- там значимых ячеек - две - отсюда и вторая строка в результате, где значения во всех колонках - становятся nan, кроме колонки PositionInObject.
df = pd.DataFrame(List_Objects)
df = df.apply(lambda x: pd.Series(x.dropna().values))
print(df.loc[:, "PositionInObject"].values)
['' '']