I need to store some values in an empty Pandas Dataframe. Something like that :
| col1 col2 col3
------------------------
row1 | v1 v2 v3
row2 | v4 v5 v6
row3 | v7 v8 v9
I get cells from other sources ex: (row2, col3, v6)
and I don't know in advance how many rows and how many columns I will have.
I tried to fill my DataFrame this way, but it's not working :
import pandas as pd
df = pd.DataFrame()
df["col1"] = ""
df["col2"] = ""
df["col3"] = ""
df["col1"].loc["row1"] = "v1"
df["col2"].loc["row2"] = "v4"
df["col3"].loc["row3"] = "v9"
# ...
When I want to display DataFrame,
print(df)
it shows as an empty DataFrame.
Empty DataFrame
Columns: [col1, col2, col3]
Index: []
Out of curiosity, I tried
print(df["col1"])
and I get :
row1 v1
row2 v4
row3 v7
Name: col1, dtype: object
And
print(df.loc["row1"])
returns a KeyError
exception.
Well, I guess I have an index problem but I don't know how to address it and I cannot use df.set_index
after first insertion since I have other constraints.
I tried this too:
df = pd.DataFrame(columns=("some_name",))
df.set_index("some_name", inplace=True)
but it failed too.
Any idea ? I think I just need to set an empty index before starting to insert data, but I don't know how.
Append Rows to Empty DataFramepandas. DataFrame. append() function is used to add the rows of other DataFrame to the end of the given DataFrame and return a new DataFrame object.
You can replace blank/empty values with DataFrame. replace() methods. The replace() method replaces the specified value with another specified value on a specified column or on all columns of a DataFrame; replaces every case of the specified value. Yields below output.
In this method, we will use “df. fillna(method='ffill')” , which is used to propagate non-null values forward or backward.
You can create an empty dataframe by importing pandas from the python library. Later, using the pd. DataFrame(), create an empty dataframe without rows and columns as shown in the below example.
This way you can add values using the pd.loc() method: import pandas as pd df = pd.DataFrame()
df["col1"] = ""
df["col2"] = ""
df["col3"] = ""
df.loc["row1", "col1"] = "v1"
df.loc["row2", "col2"] = "v4"
df.loc["row3", "col3"] = "v9"
Producing the following output:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With