Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert cells in empty Pandas DataFrame

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.

like image 537
Cyrille Avatar asked Feb 22 '19 16:02

Cyrille


People also ask

How do I add to an empty pandas DataFrame?

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.

How do I fill empty cells in a Data frame?

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.

How do I fill empty columns in pandas?

In this method, we will use “df. fillna(method='ffill')” , which is used to propagate non-null values forward or backward.

How can you create an empty DataFrame and series in pandas?

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.


1 Answers

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:

enter image description here

like image 125
Daniel Labbe Avatar answered Sep 20 '22 16:09

Daniel Labbe