Assign DataFrame to particular cell in DataFrame

I have the case when I want to nest DataFrame into another one. The problem is, I want to handle it one by one due to the way data is populated.

Some sources of documentation recommend using an .at operator to set value of particular cell. While this works well with scalar types, it causes a problem due to that it have special logic for handling assignments of Series and DataFrames.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df.at["foo","newCol"] = DataFrame(...)
</code>
<code>df.at["foo","newCol"] = DataFrame(...) </code>
df.at["foo","newCol"] = DataFrame(...)

Is there any way of bypassing this logic?

3

You can “cheat” pandas typing:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df.at["foo","newCol"] = (DataFrame(...))
</code>
<code>df.at["foo","newCol"] = (DataFrame(...)) </code>
df.at["foo","newCol"] = (DataFrame(...))

then:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df.loc["foo", "newCol"][col_name]
</code>
<code>df.loc["foo", "newCol"][col_name] </code>
df.loc["foo", "newCol"][col_name]

will return column col_name from nested DataFrame – so you will be able to interact with it as if pd.DataFrame would be a foundational pandas dtype.

6

It should be possible to achieve such result with following helper:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def update(frame, index, column, value):
def applicator(row):
if row.name == index:
return value
elif column in row:
return row[column]
else:
return None
frame[column] = frame.apply(applicator,axis=1)
</code>
<code>def update(frame, index, column, value): def applicator(row): if row.name == index: return value elif column in row: return row[column] else: return None frame[column] = frame.apply(applicator,axis=1) </code>
def update(frame, index, column, value):
    def applicator(row):
        if row.name == index:
            return value
        elif column in row:
            return row[column]
        else:
            return None
    frame[column] = frame.apply(applicator,axis=1)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>update(df,"foo","newCol",DataFrame(...))
</code>
<code>update(df,"foo","newCol",DataFrame(...)) </code>
update(df,"foo","newCol",DataFrame(...))

You can bypass it by accessing the locations using the [foo] syntax.

Let’s say you make a pandas Dataframe:
df = pd.DataFrame(index=["foo"], columns=["newCol"]), then another Dataframe called nestdf:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>nestdf = pd.Dataframe(...)
</code>
<code>nestdf = pd.Dataframe(...) </code>
nestdf = pd.Dataframe(...)

You can nest the nestdf in your df Dataframe with the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df.loc["foo", "newCol"] = nestdf
</code>
<code>df.loc["foo", "newCol"] = nestdf </code>
df.loc["foo", "newCol"] = nestdf

Now, we have the nestdf Dataframe that is nested within our df Dataframe.

However, this does not work for non-tuple or non-scalar data storage. In that case, you can use a dictionary, represented by {} brackets:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>df = pd.DataFrame({'data': [["foo","newCol"]]})
</code>
<code>df = pd.DataFrame({'data': [["foo","newCol"]]}) </code>
df = pd.DataFrame({'data': [["foo","newCol"]]})

Hope this helps!

6

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật