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)
Related
This question already has answers here:
Find column whose name contains a specific string
(8 answers)
Closed 4 months ago.
I have this type of dataframe;
A = ["axa","axb","axc","axd","bxa","bxb","bxc","bxd","cxa".......]
My question is I have this type of data but there are more than 350 columns and for example i need only 'c' including column names in new dataframe. How can i do that?
new dataframe columns should look like this;
B = A[["axc","bxc","cxa","cxb","cxc","cxd","dxc","exc","fxc".......]]
Use for filter columns names with c by DataFrame.filter:
df2 = df.filter(like='c')
Or use list comprehension for filter columns names:
df2 = df[[x for x in df.columns if 'c' in x]]
You can do it easily using list comprehension:
new_df = df[[col for col in df.columns if 'c' in col]]
This question already has answers here:
How do I select rows from a DataFrame based on column values?
(16 answers)
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
Closed 4 months ago.
I am new to writing code and currently working on a project to compare two columns of an excel sheet using python and return the rows that does not match.
I tried using the .isin funtion and was able to identify output the values comparing the columns however i am not sure on how to print the actual row that returns the value "False"
For Example:
import pandas as pd
data = ["Darcy Hayward","Barbara Walters","Ruth Fraley","Minerva Ferguson","Tad Sharp","Lesley Fuller","Grayson Dolton","Fiona Ingram","Elise Dolton"]
df = pd.DataFrame(data, columns=['Names'])
df
data1 = ["Darcy Hayward","Barbara Walters","Ruth Fraley","Minerva Ferguson","Tad Sharp","Lesley Fuller","Grayson Dolton","Fiona Ingram"]
df1 = pd.DataFrame(data1, columns=['Names'])
df1
data_compare = df["Names"].isin(df1["Names"])
for data in data_compare:
if data==False:
print(data)
However, i want to know that 8 index returned False, something like the below format
Could you please advise how i can modify the code to get the output printed with the Index, Name that returned False?
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:
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?