Super simple in-place assignment: df['new'] = 0
For in-place modification, perform direct assignment. This assignment is broadcasted by pandas for each row.
df = pd.DataFrame('x', index=range(4), columns=list('ABC'))
df
A B C
0 x x x
1 x x x
2 x x x
3 x x x
df['new'] = 'y'
# Same as,
# df.loc[:, 'new'] = 'y'
df
A B C new
0 x x x y
1 x x x y
2 x x x y
3 x x x y
Note for object columns
If you want to add an column of empty lists, here is my advice:
- Consider not doing this.
objectcolumns are bad news in terms of performance. Rethink how your data is structured. - Consider storing your data in a sparse data structure. More information: sparse data structures
If you must store a column of lists, ensure not to copy the same reference multiple times.
# Wrong df['new'] = [[]] * len(df) # Right df['new'] = [[] for _ in range(len(df))]
Generating a copy: df.assign(new=0)
If you need a copy instead, use DataFrame.assign:
df.assign(new='y')
A B C new
0 x x x y
1 x x x y
2 x x x y
3 x x x y
And, if you need to assign multiple such columns with the same value, this is as simple as,
c = ['new1', 'new2', ...]
df.assign(**dict.fromkeys(c, 'y'))
A B C new1 new2
0 x x x y y
1 x x x y y
2 x x x y y
3 x x x y y
Multiple column assignment
Finally, if you need to assign multiple columns with different values, you can use assign with a dictionary.
c = {'new1': 'w', 'new2': 'y', 'new3': 'z'}
df.assign(**c)
A B C new1 new2 new3
0 x x x w y z
1 x x x w y z
2 x x x w y z
3 x x x w y z
Answer from coldspeed95 on Stack Overflowpython - Add column with constant value to pandas dataframe - Stack Overflow
python - Add multiple columns with zero values from a list to a Pandas data frame - Stack Overflow
python - How to add column to df with zero array - Stack Overflow
pandas - how to create all zero dataframe in Python - Stack Overflow
Super simple in-place assignment: df['new'] = 0
For in-place modification, perform direct assignment. This assignment is broadcasted by pandas for each row.
df = pd.DataFrame('x', index=range(4), columns=list('ABC'))
df
A B C
0 x x x
1 x x x
2 x x x
3 x x x
df['new'] = 'y'
# Same as,
# df.loc[:, 'new'] = 'y'
df
A B C new
0 x x x y
1 x x x y
2 x x x y
3 x x x y
Note for object columns
If you want to add an column of empty lists, here is my advice:
- Consider not doing this.
objectcolumns are bad news in terms of performance. Rethink how your data is structured. - Consider storing your data in a sparse data structure. More information: sparse data structures
If you must store a column of lists, ensure not to copy the same reference multiple times.
# Wrong df['new'] = [[]] * len(df) # Right df['new'] = [[] for _ in range(len(df))]
Generating a copy: df.assign(new=0)
If you need a copy instead, use DataFrame.assign:
df.assign(new='y')
A B C new
0 x x x y
1 x x x y
2 x x x y
3 x x x y
And, if you need to assign multiple such columns with the same value, this is as simple as,
c = ['new1', 'new2', ...]
df.assign(**dict.fromkeys(c, 'y'))
A B C new1 new2
0 x x x y y
1 x x x y y
2 x x x y y
3 x x x y y
Multiple column assignment
Finally, if you need to assign multiple columns with different values, you can use assign with a dictionary.
c = {'new1': 'w', 'new2': 'y', 'new3': 'z'}
df.assign(**c)
A B C new1 new2 new3
0 x x x w y z
1 x x x w y z
2 x x x w y z
3 x x x w y z
With modern pandas you can just do:
df['new'] = 0
You could try direct assignment (assuming your dataframe is named df):
for col in l:
df[col] = 0
Or use the DataFrame's assign method, which is a slightly cleaner way of doing it if l can contain a value, an array or any pandas Series constructor.
# create a dictionary of column names and the value you want
d = dict.fromkeys(l, 0)
df.assign(**d)
Pandas Documentation on the assign method : http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html
The current accepted answer produced the following warning on my machine (using pandas=1.4.2):
PerformanceWarning: DataFrame is highly fragmented. This is usually the result of calling `frame.insert` many times, which has poor performance. Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`
I got rid of these warnings by assigning new columns like so instead:
df.loc[:, l] = 0
Pandas tries to broadcast the numpy array to fit your dataframe in ways that are not helpful in this case.
To avoid this you could make a list of the arrays.
df["matrix"] = [np.zeros((20,20)) for _ in df.index]
Or use apply to get the value you want in each cell
df["matrix"] = df.apply(lambda _: np.zeros((20, 20)), axis=1)
If you want a single column I would suggest adding a pd.Series with the desired information, such as:
df['matrix'] = [[0]*20] * len(df)
There's a trick here: when DataFrame (or Series) constructor is passed a scalar as the first argument this value is propogated:
In [11]: pd.DataFrame(0, index=np.arange(1, 4), columns=np.arange(24))
Out[11]:
0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23
1 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
[3 rows x 24 columns]
Note: np.arange is numpy's answer to python's range.
You can create an empty numpy array, convert it to a dataframe, and then add the header names.
import numpy
a = numpy.zeros(shape=(3,24))
df = pd.DataFrame(a,columns=['col1','col2', etc..])
to set row names use
df.set_index(['row1', 'row2', etc..])
if you must.
Use Setting with enlargement:
df.loc[len(df)] = 0
print (df)
A B C D E
1 1 2 0 1 0
2 0 0 0 1 -1
3 1 1 3 -5 2
4 -3 4 2 6 0
5 2 4 1 9 -1
6 0 0 0 0 0
Or DataFrame.append with Series filled by 0 and index by columns of DataFrame:
df = df.append(pd.Series(0, index=df.columns), ignore_index=True)
Create a new dataframe of zeroes using the shape and column list of the current. Then append:
df = pd.DataFrame([[1, 2], [3, 4],[5,6]], columns=list('AB'))
print(df)
A B
0 1 2
1 3 4
2 5 6
df2 = pd.DataFrame([[0]*df.shape[1]],columns=df.columns)
df = df.append(df2, ignore_index=True)
print(df)
A B
0 1 2
1 3 4
2 5 6
3 0 0
This can be accomplished by mapping a string format object to the column of floats:
df.colName.map('{:.2f}'.format)
(Credit to exp1orer)
You can use:
pd.options.display.float_format = '{:,.2f}'.format
Note that this will only display two decimals for every float in your dataframes.
To go back to normal:
pd.reset_option('display.float_format')
Create and fill a pandas dataframe with zeros
feature_list = ["foo", "bar", 37]
df = pd.DataFrame(0, index=np.arange(7), columns=feature_list)
print(df)
which prints:
foo bar 37
0 0 0 0
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 0 0
It's best to do this with numpy in my opinion
import numpy as np
import pandas as pd
d = pd.DataFrame(np.zeros((N_rows, N_cols)))
You can create the append df with concat
out = pd.concat([df,pd.DataFrame({'D':[7]})]).fillna(0)
out
A B C D
0 1.0 2.0 3.0 0.0
1 4.0 5.0 6.0 0.0
0 0.0 0.0 0.0 7.0
Other solution, with .append:
print(df.append({"D": 7}, ignore_index=True).fillna(0).astype(int))
Prints:
A B C D
0 1 2 3 0
1 4 5 6 0
2 0 0 0 7