Creating an Empty Pandas Dataframe, Then Filling It

Creating an empty Pandas DataFrame, and then filling it

Here's a couple of suggestions:

Use date_range for the index:

import datetime
import pandas as pd
import numpy as np

todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date-datetime.timedelta(10), periods=10, freq='D')

columns = ['A','B', 'C']

Note: we could create an empty DataFrame (with NaNs) simply by writing:

df_ = pd.DataFrame(index=index, columns=columns)
df_ = df_.fillna(0) # With 0s rather than NaNs

To do these type of calculations for the data, use a NumPy array:

data = np.array([np.arange(10)]*3).T

Hence we can create the DataFrame:

In [10]: df = pd.DataFrame(data, index=index, columns=columns)

In [11]: df
Out[11]:
A B C
2012-11-29 0 0 0
2012-11-30 1 1 1
2012-12-01 2 2 2
2012-12-02 3 3 3
2012-12-03 4 4 4
2012-12-04 5 5 5
2012-12-05 6 6 6
2012-12-06 7 7 7
2012-12-07 8 8 8
2012-12-08 9 9 9

How to fill cell by cell of an empty pandas dataframe which has zero columns with a loop?

You can solve this by using enumerate(), together with loc:

for index, i in enumerate(cids):
url_info = requests.get(f'myurl/{i}/profile')
jdata = url_info.json()
df.loc[index, 'Customer_id'] = i
df.loc[index, 'Name'] = jdata['user']['profile']['Name']

Filling all of an empty dataframe's rows or columns with a single series in pandas

You can use np.broadcast_to, last if necessary transpose ouput for second sample data:

s = pd.Series([3,4,5])
N = 2
M = 3

df = pd.DataFrame(index=range(N), columns=range(M))

df[:] = np.broadcast_to(s.to_numpy(), (N, M))
print (df)
0 1 2
0 3 4 5
1 3 4 5

s = pd.Series([10, 11])
N = 2
M = 3

df = pd.DataFrame(index=range(N), columns=range(M))
df[:] = np.broadcast_to(s.to_numpy(), (M, N)).T
print (df)
0 1 2
0 10 10 10
1 11 11 11


s = pd.Series([3,4,5])
N = 2
M = 3

df = pd.DataFrame(np.broadcast_to(s.to_numpy(), (N, M)))
print (df)
0 1 2
0 3 4 5
1 3 4 5

s = pd.Series([10, 11])
N = 2
M = 3

df = pd.DataFrame(np.broadcast_to(s.to_numpy(), (M, N)).T)
print (df)
0 1 2
0 10 10 10
1 11 11 11

How to make empty Pandas DataFrame with named columns and then add a row?

data_set = pd.DataFrame(columns=['POST_TEXT', 'TARGET'])

# output
Empty DataFrame
Columns: [POST_TEXT, TARGET]
Index: []

# add row
data_set = data_set.append({"POST_TEXT": 5, "TARGET": 10}, ignore_index=True)

# output
POST_TEXT TARGET
0 5 10

So to append row you have to define dict where key is name of the column and value is the value you want to append.

If you would like to add row and populate only one column:

data_set = data_set.append({"POST_TEXT": 50}, ignore_index=True)

# output
POST_TEXT TARGET
0 50.0 NaN

How to create an empty dataframe

You're seeing the result of how an empty DataFrame is displayed,. The DataFrame is in fact empty; you can see this better using a print or checking the empty attribute.

import pandas as pd
df = pd.DataFrame()

display(df)
#_

print(df)
#Empty DataFrame
#Columns: []
#Index: []

df.empty
#True

Filling empty dataframe with loop

you can save your filter results (which are correct) in a list, then use pd.concat to get the new df.

try this:

filtered = []
for i in list_holidays:
filter = df[df['date'].str.contains(i)]
filtered.append(filter)

new_df = pd.concat(filtered)

print(new_df)

or with a simple list-comprehension:

new_df = pd.concat([df[df['date'].str.contains(i)] for i in list_holidays])

print(new_df)

Creating an empty dataframe with 50 columns with only 5 specific columns filled

import pandas as pd

a = pd.DataFrame({"id": [1, 2], "order": [111, 222], "first": ["Johnny", "Amber"], "last": ["Depp", "Heard"], "type": ["type1", "type2"]})
push = ["x", "order", "first", "last"] + list("x" * 7) + ["type"] + list("x" * 4)
cols = [f"x{num}" if value == "x" else value for num, value in enumerate(push)]
b = pd.DataFrame({col: a[col] if col in a.columns.to_list() else None for col in cols})
print(b)

Seems like a fairly arbitrary problem, but I think this solves your specific request. Feel free to change the "x" * 7 value to reflect your wishes. Also you can replace None with np.nan if you import numpy as np. Or you could replace None with "" to insert empty strings. Your questions is a bit vague by stating "empty".

Output:

     x0  order   first   last    x4    x5    x6    x7    x8    x9   x10   type   x12   x13   x14   x15
0 None 111 Johnny Depp None None None None None None None type1 None None None None
1 None 222 Amber Heard None None None None None None None type2 None None None None

How to create a empty dataframe and appened it

DataFrame.append is not list.append. You need to assign the result back.

dataframe = dataframe.append(df)

However, appending within a loop is not advised as it needlessly copies data. You should append to a list and concatenate once in the end. We can turn the loop into a list comprehension within concat.

import pandas as pd

dataframe = pd.concat([pd.read_csv(name) for name in list])

Elegant way to create empty pandas DataFrame with NaN of type float

Simply pass the desired value as first argument, like 0, math.inf or, here, np.nan. The constructor then initializes and fills the value array to the size specified by arguments index and columns:

>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame(np.nan, index=[0, 1, 2, 3], columns=['A', 'B'])

>>> df
A B
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN

>>> df.dtypes
A float64
B float64
dtype: object


Related Topics



Leave a reply



Submit