Pandas - create dataframe manually and insert values
Providing a sample to increase your data frame dynamically... sizeOfDataFrame variable just limits for loop which adds data to the dataframe and is dynamic...
import pandas as pd
import numpy as np
yourDataFrame = pd.DataFrame()
sizeOfDataFrame = np.random.randint(100, size=1)
for currentLine in range(sizeOfDataFrame):
yourDataFrame = yourDataFrame.append(pd.DataFrame({"A":np.random.randint(100, size=1),"B":np.random.randint(100, size=1),"C":np.random.randint(100, size=1)},index=[0]))
yourDataFrame.reset_index(inplace = True)
yourDataFrame.drop('index',axis=1,inplace=True)
You can either initialize dataframe with data using
df = pd.DataFrame(columns=["A", "B"], data=[[5,np.nan]])
,
or use set_value
method (which is much faster than iloc
by the way):
df.set_value(0,'A',5)
UPDATE 2018-04-12 ⬇
Since pandas version 0.21.0 df.set_value
is deprecated. You should use .at[]
or .iat[]
accessors instead:
df.at[0, 'A'] = 5