-1

How to add a list of values to the end of dataframe that already has values? I tried append without any luck. dataAlkuperainen are pandas csv files that I turned into dataframes. But for some reason appending them wont do anything.

   # collecting data from a csv file
dataAlkuperainen = pd.read_csv("persons_customers_AT.csv", 
sep=";", dtype={"counterparty_id":str, "counterparty_firstname": str, "counterparty_lastname":str, 
"counterparty_type":str, "date_of_birth":str},  encoding='ISO-8859-1', engine = 'python')

dataAlkuperainen2 = pd.read_csv("Organisations_customers_AT.csv", 
sep=";", dtype={"counterparty_id":str, "organisation_name": str},  encoding='ISO-8859-1', engine = 'python')

dateOfBirth = dataAlkuperainen["date_of_birth"]

# creating dataframe with new columns
df = DataFrame(dataAlkuperainen, columns=[ "counterparty_id", "organisation_name" ,"counterparty_firstname", "counterparty_lastname", 
"counterparty_type", "vuosi", "kuukausi", "paiva"], dtype=object)

df2 = DataFrame(dataAlkuperainen2, columns=[ "counterparty_id", "organisation_name"], dtype=object)
df.astype(str)

# slicing date of birth to separate selections
vuosi = []
kk = []
paiva = []
for x in dateOfBirth:
    vuosi.append(x[:4])
    kk.append(x[5:7])
    paiva.append(x[8:10])

idObject = {"ID", "Organisation_name"}
listOfCompanies = []
idx = 0
for x, y in dataAlkuperainen2['counterparty_id'].items():
    idObject = {"ID": y, "Organisation_name": dataAlkuperainen2['organisation_name'][x]}
    listOfCompanies.append(idObject)

df['vuosi'] = vuosi
df['kuukausi'] = kk
df['paiva'] = paiva

df['counterparty_id'].loc[len(df)] = df2['counterparty_id'].values[:df.shape[1]]
export_csv = df.to_csv (r'./test.cvs', index = None, sep=";", header=True, encoding="ISO-8859-1")
print(df2)
print(df)
10
  • Does this answer your question? add row to dataframe pandas Commented Aug 18, 2021 at 12:05
  • no I need to add to the end of my dataframe values from another dataframe values to specific column. Commented Aug 18, 2021 at 12:10
  • 2
    can you provide sample dataframe and your expected output from it? Commented Aug 18, 2021 at 12:11
  • 1
    Sir your dataframe is non reproducible Commented Aug 18, 2021 at 12:42
  • 1
    What is dataAlkuperainen2? Is it a dictionary, that has the same structure as dataAlkuperainen? If so, then make a DataFrame out of it before append-ing. (You need to provide more information about the structures and also the errors you ran into.) Commented Aug 18, 2021 at 15:18

1 Answer 1

0

I suppose, you want to take column from df2 and put it as a row into df1. You can just add a new row. For example, use loc:

Update. Consider different lenghts:

d = {"A":pd.Series([1, 2, 3], index=[1,2,3]), "B": pd.Series([10, 12, 17], index=[1,2,3])}
df1 = pd.DataFrame(d)

df1

    A   B
1   1   10
2   2   12
3   3   17


d = {"B":pd.Series([1, 22, 111], index=[1,2, 3]), "C": pd.Series([0, 0, 0], index=[1,2, 3])}
df2 = pd.DataFrame(d)
df2


    B   C
1   1   0
2   22  0
3   111 0


df1.loc[4] = df2['C'].values[:df1.shape[1]]
df1


    A   B
1   1   10
2   2   12
3   3   17
4   0   0
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but it didnt work. My code is df.loc[len(df)] = dataAlkuperainen2['counterparty_id'].values but I get ValueError: cannot set a row with mismatched columns error
@Levsha then define the length: df1.loc[4] = df2['C'].values[:df1.shape[1]]
this line df.loc[len(df)] = dataAlkuperainen2['counterparty_id'].values[:df.shape[1]] didn't add values at the end of the dataframe. For some reason nothing happened

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.