You can convert DataFrames to numpy arrays:
for i, (x, y) in enumerate(zip(df1.values, df2.values)):
print(i, x, y)
Your solution return columns names, becuse is processes like:
for i, (x, y) in enumerate(zip(df1.columns, df2.columns)):
print(i, x, y)
In Python 3, zip function creates a zip object, which is a generator and we can use it to produce one item at a time. To get a list of tuples, we can use list() and create a list of tuples.,If you are a regular computer user, you should have used .zip file extension. Do you know what it is? Basically, .zip is a container itself. It holds the real file inside.
list1 = ['Alpha', 'Beta', 'Gamma', 'Sigma'] list2 = ['one', 'two', 'three', 'six'] test = zip(list1, list2) # zip the values print('\nPrinting the values of zip') for values in test: print(values) # print each tuples
test = zip(list1, list2)
[]
print((test))
pd.DataFrame([1, 2, 3], index = [1, 1, 1])
pandas 0.24.2 documentation »,Perform column-wise combine with another DataFrame based on a passed function.,Function that takes two series as inputs and return a Series or a scalar. Used to merge the two dataframes column by columns.,Combines a DataFrame with other DataFrame using func to element-wise combine columns. The row and column indexes of the resulting DataFrame will be the union of the two.
>>> df1 = pd.DataFrame({
'A': [0, 0],
'B': [4, 4]
}) >>>
df2 = pd.DataFrame({
'A': [1, 1],
'B': [3, 3]
}) >>>
take_smaller = lambda s1, s2: s1
if s1.sum() < s2.sum()
else s2 >>>
df1.combine(df2, take_smaller)
A B
0 0 3
1 0 3
>>> df1 = pd.DataFrame({
'A': [5, 0],
'B': [2, 4]
}) >>>
df2 = pd.DataFrame({
'A': [1, 1],
'B': [3, 3]
}) >>>
df1.combine(df2, np.minimum)
A B
0 1 2
1 0 3
>>> df1 = pd.DataFrame({
'A': [0, 0],
'B': [None, 4]
}) >>>
df2 = pd.DataFrame({
'A': [1, 1],
'B': [3, 3]
}) >>>
df1.combine(df2, take_smaller, fill_value = -5)
A B
0 0 - 5.0
1 0 4.0
>>> df1 = pd.DataFrame({
'A': [0, 0],
'B': [None, 4]
}) >>>
df2 = pd.DataFrame({
'A': [1, 1],
'B': [None, 3]
}) >>>
df1.combine(df2, take_smaller, fill_value = -5)
A B
0 0 NaN
1 0 3.0
>>> df1 = pd.DataFrame({
'A': [0, 0],
'B': [4, 4]
}) >>>
df2 = pd.DataFrame({
'B': [3, 3],
'C': [-10, 1],
}, index = [1, 2]) >>>
df1.combine(df2, take_smaller)
A B C
0 NaN NaN NaN
1 NaN 3.0 - 10.0
2 NaN 3.0 1.0
>>> df1.combine(df2, take_smaller, overwrite = False) A B C 0 0.0 NaN NaN 1 0.0 3.0 - 10.0 2 NaN 3.0 1.0
In this episode we will consider different scenarios and show we might join the data. We will use csv files and in all cases the first step will be to read the datasets into a pandas Dataframe from where we will do the joining. The csv files we are using are cut down versions of the SN7577 dataset to make the displays more manageable.,The data can be related to each other in different ways. How they are related and how completely we can join the data from the datasets will vary.,How can I join two Dataframes with a common key?,How can I join two Dataframes with a common key?
import pandas as pd
df_SN7577i_a = pd.read_csv("SN7577i_a.csv")
df_SN7577i_b = pd.read_csv("SN7577i_b.csv")
print(df_SN7577i_a) print(df_SN7577i_b)
Id Q1 Q2 Q3 Q4 0 1 1 - 1 1 8 1 2 3 - 1 1 4 2 3 10 3 2 6 3 4 9 - 1 10 10 ... Id Q1 Q2 Q3 Q4 0 1277 10 10 4 6 1 1278 2 - 1 5 4 2 1279 2 - 1 4 5 3 1280 1 - 1 2 3 ...
df_all_rows = pd.concat([df_SN7577i_a, df_SN7577i_b]) df_all_rows
df_all_rows = df_all_rows.reset_index(drop = True) # or, alternatively, there 's the `ignore_index` option in the `pd.concat()` function: df_all_rows = pd.concat([df_SN7577i_a, df_SN7577i_b], ignore_index = True) df_all_rows
df_SN7577i_aa = pd.read_csv("SN7577i_aa.csv")
df_SN7577i_bb = pd.read_csv("SN7577i_bb.csv")
df_all_rows = pd.concat([df_SN7577i_aa, df_SN7577i_bb])
df_all_rows
August 31, 2021March 4, 2022
For example, you’re given two lists: list_a
, which contains [1,2,3,4]
and list_b
, which contains ['a', 'b', 'c', 'd']
. If you were to zip these two lists, you’d get back the following:
# Understanding the Python zip() function list_a = [1, 2, 3, 4] list_b = ['a', 'b', 'c', 'd'] print(list(zip(list_a, list_b))) # Returns: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
Now, you may notice that we’ve wrapped the zip() function with the list()
function. The reason for this is that the zip function actually returns a zip
object. We can confirm this by checking the type of this:
# What is a zip object?
list_a = [1,2,3,4]
list_b = ['a', 'b', 'c', 'd']
zipped = zip(list_a, list_b)
print(zipped)
# Returns: <zip object at 0x00000163B0D53FC0>
In the example below, you’ll learn how to zip two lists in:
list_a = [1, 2, 3, 4] list_b = ['a', 'b', 'c', 'd'] zipped = zip(list_a, list_b) zipped_list = list(zipped) print(zipped_list) # Returns: [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
This returns:
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
In the example below, you’ll learn how to zip three lists and then return it as a list of tuples, using the built-in list()
function:
list_a = [1, 2, 3, 4] list_b = ['a', 'b', 'c', 'd'] list_c = [99, 88, 77, 66] zipped = zip(list_a, list_b, list_c) zipped_list = list(zipped) print(zipped_list) # Returns: [(1, 'a', 99), (2, 'b', 88), (3, 'c', 77), (4, 'd', 66)]