This question already has answers here:
Appending to an empty DataFrame in Pandas?
(5 answers)
Creating an empty Pandas DataFrame, and then filling it
(8 answers)
Closed 3 years ago.
I am trying to append a new row to an empty dataset and i found the below code fine:
import panda as pd
df = pd.DataFrame(columns=['A'])
for i in range(5):
df = df.append({'A': i}, ignore_index=True)
So, it gives me:
A
0 0
1 1
2 2
3 3
4 4
But, when i try the below code, my dataset is still empty:
df = pd.DataFrame(columns=['A'])
df.append({'A': 2}, ignore_index=True)
df
Can someone explain me the solution to add only 1 row?
Related
This question already has answers here:
How do I count the values from a pandas column which is a list of strings?
(5 answers)
Closed 11 months ago.
I am trying to count the number of characters in an uneven 2-D pandas series.
df = pd.DataFrame({ 'A' : [['a','b'],['a','c','f'],['a'], ['b','f']]}
I want to count the number of times each character is repeated.
any ideas?
You can use explode() and value_counts().
import pandas as pd
df = pd.DataFrame({ 'A' : [['a','b'],['a','c','f'],['a'], ['b','f']]})
df = df.explode("A")
print(df.value_counts())
Expected output:
A
a 3
b 2
f 2
c 1
This question already has answers here:
Pandas Merging 101
(8 answers)
Closed 11 months ago.
I have a dataset df with this shape (1038828, 6)
lib_source = df['LIB_SOURCE']
lib_source = pd.get_dummies(lib_source, prefix='source', prefix_sep='_')
lib_source has the same shape : (1038828, 2)
After I merge lib_source with df :
df = df.join(lib_source)
And now df shape is (1777590, 7).
Why please I don'k keep the initial shape please?
Thanks.
Try using pd.concat instead:
df = pd.concat([df, lib_source], axis=1)
This question already has answers here:
Pandas Merging 101
(8 answers)
Pandas: how to merge two dataframes on a column by keeping the information of the first one?
(4 answers)
Closed 2 years ago.
I am trying to append values into a new pandas dataframe (df_t2) by looping over df. Assign does not seem to work. Is there any other way to achieve this?
values = {'No': ['123','456'],
'data1': [22000,25000]}
df = pd.DataFrame(values, columns = ['No', 'data1'])
Code to loop over dataframe (df) and copy it to df_t2:
for index, row in df.iterrows():
df_t2["No"] = row['No']
df_t2[str(row['point1']) = row['data1']
Try reading here:
https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html
import pandas as pd
values = {'No1': ['123','4562'], 'data1': [22000,21000] }
df_1 = pd.DataFrame(values , columns = ['No1', 'data1'])
values = {'No2': ['1231','456'], 'data2': [24000,25000] }
df_2 = pd.DataFrame(values , columns = ['No2', 'data2'])
all_values = np.concatenate([df_1.values,df_2.values])
new_df_2 = pd.DataFrame(all_values, columns = df_2.columns)
This question already has answers here:
How to replace NaN values by Zeroes in a column of a Pandas Dataframe?
(17 answers)
Closed 2 years ago.
df_train[catcols] = df_train[catcols].fillna("NANO")
df_test[catcols[:-2]] = df_test[catcols[:-2]].fillna("NANO")
fillna is a method in pandas series and dataframe. It replaces NA/NAN values in a dataframe.
Syntax :
df.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
In this case
df_train[catcols] = df_train[catcols].fillna("NANO")
df_test[catcols[:-2]] = df_test[catcols[:-2]].fillna("NANO")
'NANO' string is replaces where a NONE value is found in the dataframe.
For example:
if your dataframe df is :
Index Name
1 Jacob
2 Andrew
3 NONE
4 NONE
5 Steve
df['Name'].fillna('Aagam')
Index Name
1 Jacob
2 Andrew
3 Aagam
4 Aagam
5 Steve
to learn more visit Pandas.dataframe.fillna
This question already has answers here:
How to reset index in a pandas dataframe? [duplicate]
(3 answers)
Closed 4 years ago.
I have group by data set but I'm unable to convert it to json. It throws out json with a bad format. TO_excel works fine.
Country Sub amount
3 source4
UK 1 source3
1 source1
US 2 source2
How can I export groupby dataset to_json?
There is problem you have MultiIndex in DataFrame, so need reset_index:
j = df.reset_index().to_json()
print (j)
{"Country":{"0":"UK","1":"UK","2":"US"},
"Sub":{"0":1,"1":1,"2":2},
"amount":{"0":"source3","1":"source1","2":"source2"}}