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.
df.at["foo","newCol"] = DataFrame(...)
Is there any way of bypassing this logic?
3
You can “cheat” pandas
typing:
df.at["foo","newCol"] = (DataFrame(...))
then:
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:
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)
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:
nestdf = pd.Dataframe(...)
You can nest the nestdf
in your df
Dataframe with the following:
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:
df = pd.DataFrame({'data': [["foo","newCol"]]})
Hope this helps!
6